如何在Java中拆分字符串

发布于 2025-01-17 21:44:13 字数 390 浏览 0 评论 0 原文

我有 String = March2022 我想将其分成两个词 March 和 2022,但 March 一词不断变化,具体取决于月份(有时是 4 月或 6 月)以及年份(有时是 2023 或 2024),我该如何将其拆分为

String month = March
String year = 2022

String month = April
String year = 2024

提前感谢

我尝试过

month.split("\[0-9\]")\[0\];
year.split("\[A-z\]")\[0\];

但没有工作

I have String = March2022
and I want to split it into two words March and 2022 but the March word keeps changing based on the month sometimes April or June as well the year sometimes 2023 or 2024, how can I split it to

String month = March
String year = 2022

or

String month = April
String year = 2024

thanks in advance

I tried

month.split("\[0-9\]")\[0\];
year.split("\[A-z\]")\[0\];

but does not work

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

公布 2025-01-24 21:44:13

您不需要拆分代码(带有分离器),而需要匹配的代码

String value = "March2022";
Matcher m = Pattern.compile("([a-zA-Z]+)(\\d+)").matcher(value);

if (m.find()) {
    String month = m.group(1);
    String year = m.group(2);
    System.out.println(month + "/" + year); // March/2022
}

You don't need a splitting code (with separator), but a matching one

String value = "March2022";
Matcher m = Pattern.compile("([a-zA-Z]+)(\\d+)").matcher(value);

if (m.find()) {
    String month = m.group(1);
    String year = m.group(2);
    System.out.println(month + "/" + year); // March/2022
}
夏至、离别 2025-01-24 21:44:13

如果我理解正确的话,这就是您正在寻找的:

String date = "March2022";

String[] part = date.split("(?<=\\D)(?=\\d)");
String month = part[0];
String year = part[1];

If I understand you correctly this is what you are looking for:

String date = "March2022";

String[] part = date.split("(?<=\\D)(?=\\d)");
String month = part[0];
String year = part[1];
无尽的现实 2025-01-24 21:44:13

您可以使用常规的表格来捕获月份的名称和年份:

String monthYear =  "March2022";
        
// Create pattern to match strings with the month name followed by the year
Pattern pattern = Pattern.compile("([A-Za-z]+)([0-9]+)");
        
// Applies the pattern to the String inputed
Matcher matcher = pattern.matcher(monthYear);

if (matcher.find()) {
    String month = matcher.group(1); // gets the value captured by the first brackets -> ([A-Za-z]+)
    String year = matcher.group(2); // gets the value captured by the second brackets -> ([0-9]+)
}

您可以在此处阅读更多文档:

You can use a regular expresion to capture the month name and year:

String monthYear =  "March2022";
        
// Create pattern to match strings with the month name followed by the year
Pattern pattern = Pattern.compile("([A-Za-z]+)([0-9]+)");
        
// Applies the pattern to the String inputed
Matcher matcher = pattern.matcher(monthYear);

if (matcher.find()) {
    String month = matcher.group(1); // gets the value captured by the first brackets -> ([A-Za-z]+)
    String year = matcher.group(2); // gets the value captured by the second brackets -> ([0-9]+)
}

You can read further documentation here: https://www.w3schools.com/java/java_regex.asp

苹果你个爱泡泡 2025-01-24 21:44:13

我想指出的是,这里没有真正的正则表达式。

int index = 0;
while (index < monthYear.length() && !Character.isDigit(monthYear.charAt(index))) {
    ++index;
}
String month = monthYear.substring(0, index);
String year = monthYear.substring(index);

这将“将“ 2022年3月”分为“ 3月”和“ 2022”或“ 2022年”或“ 11月2023年”,为“ 11月”和“ 2023”或“ 2023”或“ 5月2024年”,到“ 5月”和“ 2024”等。

如果您不关心” 9999年“问题,您也可以做到:

String month = monthYear.substring(0, monthYear.length() - 4);
String year = monthYear.substring(monthYear.length() - 4);

尽管如果字符串为空,或者只包含一个月,并且一个月短于4个字符(或者,通常,如果字符串短于4个字符)。

I would just like to point out that there's no real need for regular expressions here.

int index = 0;
while (index < monthYear.length() && !Character.isDigit(monthYear.charAt(index))) {
    ++index;
}
String month = monthYear.substring(0, index);
String year = monthYear.substring(index);

This will split "March2022" into "March" and "2022" or "November2023" into "November" and "2023" or "May2024" into "May" and "2024", etc.

If you don't care about the "Year 9999" problem, you can also just do:

String month = monthYear.substring(0, monthYear.length() - 4);
String year = monthYear.substring(monthYear.length() - 4);

Although this will throw if the string is empty, or only contains the month and the month is shorter than 4 characters (or, generally, if the string is shorter than 4 characters).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文