Java字符串分割不带空格

发布于 2024-11-06 13:53:02 字数 124 浏览 1 评论 0原文

我正在尝试拆分一些用户输入。输入的形式为 a1 b2 c3 d4。 对于每个输入(例如a1),如何将其拆分为“a”和“1”?

我熟悉字符串分割函数,但是我应该指定什么作为分隔符,或者这是否可能?

谢谢。

I'm trying to split some user input. The input is of the form a1 b2 c3 d4.
For each input (eg; a1), how do I split it into 'a' and '1'?

I'm familiar with the string split function, but what do I specify as the delimiter or is this even possible?

Thanks.

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

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

发布评论

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

评论(7

誰ツ都不明白 2024-11-13 13:53:02

您可以使用 String#substring()

String a1 = "a1"
String firstLetterStr = a1.substring(0,1);
String secondLetterStr = a1.substirng(1,a1.length());

类似地,

String c31 = "c31"
String firstLetterStr = c31.substring(0,1);
String secondLetterStr = c31.substirng(1,c31.length());

You could use String#substring()

String a1 = "a1"
String firstLetterStr = a1.substring(0,1);
String secondLetterStr = a1.substirng(1,a1.length());

Similarly,

String c31 = "c31"
String firstLetterStr = c31.substring(0,1);
String secondLetterStr = c31.substirng(1,c31.length());
勿挽旧人 2024-11-13 13:53:02

如果您想一般地拆分字符串(而不是尝试根据其他答案计算字符数),您仍然可以使用 String.split(),但必须使用 正则表达式。 (注意:当您有 a1、a2、aaa333 等字符串时,此答案将起作用)

String ALPHA = "\p{Alpha}";
String NUMERIC = "\d";

String test1 = "a1";
String test2 = "aa22";

ArrayList<String> alpha = new ArrayList();
ArrayList<String> numeric = new ArrayList();

alpha.add(test1.split(ALPHA));
numeric.add(test1.split(NUMERIC));
alpha.add(test2.split(ALPHA));
numeric.add(test2.split(NUMERIC));

此时,alpha 数组将包含字符串的 alpha 部分,而数字数组将包含数字部分。 (注意:我实际上并没有编译它来测试它是否有效,但它应该为您提供基本的想法。)

If you want to split the string generically (rather than trying to count characters per the other answers), you can still use String.split(), but you have to utilize regular expressions. (Note: This answer will work when you have strings like a1, a2, aaa333, etc.)

String ALPHA = "\p{Alpha}";
String NUMERIC = "\d";

String test1 = "a1";
String test2 = "aa22";

ArrayList<String> alpha = new ArrayList();
ArrayList<String> numeric = new ArrayList();

alpha.add(test1.split(ALPHA));
numeric.add(test1.split(NUMERIC));
alpha.add(test2.split(ALPHA));
numeric.add(test2.split(NUMERIC));

At this point, the alpha array will have the alpha parts of your strings and the numeric array will have the numeric parts. (Note: I didn't actually compile this to test that it would work, but it should give you the basic idea.)

百变从容 2024-11-13 13:53:02

这实际上取决于您之后将如何使用数据,但除了 split("") 或通过索引访问单个字符之外,另一种拆分为单个字符的方法是 toCharArray()——它只是将字符串分解成字符数组......

it really depends how you're going to use the data afterwards, but besides split("") or accessing individual characters by index, one other way to split into individual character is toCharArray() -- which just breaks the string into an array of characters...

Smile简单爱 2024-11-13 13:53:02

是的,有可能,您可以使用 split("");

Yes, it is possible, you can use split("");

嘦怹 2024-11-13 13:53:02

使用 split(" ") 将用户输入拆分为单个标记后,您可以使用 split("") 将每个标记拆分为字符(使用空字符串作为分隔符) )。

After you split user input into individual tokens using split(" "), you can split each token into characters using split("") (using the empty string as the delimiter).

却一份温柔 2024-11-13 13:53:02

将空间拆分为字符串数组,然后使用 String.charAt(0)String.charAt(1) 提取各个字符

Split on space into an array of Strings, then pull the individual characters with String.charAt(0) and String.charAt(1)

<逆流佳人身旁 2024-11-13 13:53:02

我建议只对三个字符进行迭代。

for(int i = 0; i < str.length(); i += 3) {
     char theLetter = str.charAt(i);
     char theNumber = str.charAt(i + 1);
     // Do something
}

编辑:如果它可以是多个字母或数字,请使用正则表达式:

([a-z]+)(\d+)

信息:http:// www.regular-expressions.info/java.html

I would recommend just iterating over the characters in threes.

for(int i = 0; i < str.length(); i += 3) {
     char theLetter = str.charAt(i);
     char theNumber = str.charAt(i + 1);
     // Do something
}

Edit: if it can be more than one letter or digit, use regular expressions:

([a-z]+)(\d+)

Information: http://www.regular-expressions.info/java.html

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