java Java字符串没有空格分割
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5944680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java string split without space
提问by Triple777er
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'?
我正在尝试拆分一些用户输入。输入的形式为 a1 b2 c3 d4。对于每个输入(例如;a1),我如何将其拆分为“a”和“1”?
I'm familiar with the string split function, but what do I specify as the delimiter or is this even possible?
我熟悉字符串拆分函数,但是我应该指定什么作为分隔符,或者这是否可能?
Thanks.
谢谢。
采纳答案by JasCav
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.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));
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.)
此时,alpha 数组将包含字符串的 alpha 部分,而数字数组将包含数字部分。(注意:我实际上并没有编译它来测试它是否有效,但它应该给你基本的想法。)
回答by Bala R
You could use String#substring()
你可以使用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());
回答by scaevity
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...
这实际上取决于您之后将如何使用数据,但是除了split("")
通过索引访问单个字符之外,另一种拆分为单个字符的方法是toCharArray()
- 它将字符串分解为一个字符数组......
回答by wespiserA
Yes, it is possible, you can use split("");
是的,这是可能的,您可以使用 split("");
回答by Ted Hopp
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).
使用 将用户输入拆分为单个标记后split(" ")
,您可以使用split("")
(使用空字符串作为分隔符)将每个标记拆分为字符。
回答by Brian Roach
Split on space into an array of Strings, then pull the individual characters with String.charAt(0)
and String.charAt(1)
将空间拆分为字符串数组,然后使用String.charAt(0)
和拉出单个字符String.charAt(1)
回答by Ry-
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