我需要通过空格来分割字符串。 为此,我尝试了以下方法:
str = "Hello I'm your String";
String[] splited = str.split(" ");
但是它似乎不起作用。
我需要通过空格来分割字符串。 为此,我尝试了以下方法:
str = "Hello I'm your String";
String[] splited = str.split(" ");
但是它似乎不起作用。
你的代码应该是可以工作的。但是,如果提供的空格默认为其他字符,你可以使用空白字符的正则表达式:
str = "Hello I'm your String";
String[] splited = str.split("\\s+");
这会导致任意数量的连续空格将把您的字符串分成标记。List<String>
作为结果(因为通常我们应该优先使用List
而不是数组),该怎么办呢?当然,我可以进行转换,但我能直接解析成List
而避免中间数组吗? - Oleg VazhnevArrays.asList(str.split("\\s+"));
- 或者也许是 Stream.of(str.split("\\s+"))
并对流进行操作。 - corsiKa虽然已经有了一个很好的答案,但要注意如果输入字符串以空格开头,你将会得到一个前导空字符串。例如:
String str = " Hello I'm your String";
String[] splitStr = str.split("\\s+");
结果将是:
splitStr[0] == "";
splitStr[1] == "Hello";
splitStr[2] == "I'm";
splitStr[3] == "Your";
splitStr[4] == "String";
因此,在拆分字符串之前,您可能希望对其进行修剪:
String str = " Hello I'm your String";
String[] splitStr = str.trim().split("\\s+");
[编辑]
除了要注意trim
的问题外,您可能还需要考虑Unicode中的不间断空格字符(U+00A0
)。这个字符在字符串中打印出来就像普通空格一样,并且经常隐藏在从富文本编辑器或网页复制粘贴的文本中。它们不能被.trim()
处理,因为.trim()
使用c <= ' '
测试要删除的字符;也无法用\s
捕获它们。
相反,您可以使用\p{Blank}
,但您还需要启用Unicode字符支持,而常规的split
不会这样做。例如,这将起作用:Pattern.compile("\\p{Blank}", UNICODE_CHARACTER_CLASS).split(words)
,但它不会做trim
部分。
以下演示了该问题并提供了解决方案。虽然使用正则表达式远非最佳选择,但现在Java具有8位/16位字节表示,因此对于这个问题的有效解决方案变得相当复杂。
public class SplitStringTest
{
static final Pattern TRIM_UNICODE_PATTERN = Pattern.compile("^\\p{Blank}*(.*)\\p{Blank}$", UNICODE_CHARACTER_CLASS);
static final Pattern SPLIT_SPACE_UNICODE_PATTERN = Pattern.compile("\\p{Blank}", UNICODE_CHARACTER_CLASS);
public static String[] trimSplitUnicodeBySpace(String str)
{
Matcher trimMatcher = TRIM_UNICODE_PATTERN.matcher(str);
boolean ignore = trimMatcher.matches(); // always true but must be called since it does the actual matching/grouping
return SPLIT_SPACE_UNICODE_PATTERN.split(trimMatcher.group(1));
}
@Test
void test()
{
String words = " Hello I'm\u00A0your String\u00A0";
// non-breaking space here --^ and there -----^
String[] split = words.split(" ");
String[] trimAndSplit = words.trim().split(" ");
String[] splitUnicode = SPLIT_SPACE_UNICODE_PATTERN.split(words);
String[] trimAndSplitUnicode = trimSplitUnicodeBySpace(words);
System.out.println("words: [" + words + "]");
System.out.println("split: [" + Arrays.stream(split).collect(Collectors.joining("][")) + "]");
System.out.println("trimAndSplit: [" + Arrays.stream(trimAndSplit).collect(Collectors.joining("][")) + "]");
System.out.println("splitUnicode: [" + Arrays.stream(splitUnicode).collect(Collectors.joining("][")) + "]");
System.out.println("trimAndSplitUnicode: [" + Arrays.stream(trimAndSplitUnicode).collect(Collectors.joining("][")) + "]");
}
}
结果为:
words: [ Hello I'm your String ]
split: [][Hello][I'm your][String ]
trimAndSplit: [Hello][I'm your][String ]
splitUnicode: [][Hello][I'm][your][String]
trimAndSplitUnicode: [Hello][I'm][your][String]
我认为在str.split括号中放置一个正则表达式可以解决这个问题。Java的String.split()方法基于正则表达式,所以你需要的是:
str = "Hello I'm your String";
String[] splitStr = str.split("\\s+");
使用Stringutils.split()
方法按照空格拆分字符串。例如,StringUtils.split("Hello World")
返回"Hello"和"World";
为了解决提到的情况,我们可以像这样使用split方法
String split[]= StringUtils.split("Hello I'm your String");
当我们打印分割后的数组时,输出将是:
Hello
I'm
your
String
String[] splited = str.split("\\s");
http://download.oracle.com/javase/tutorial/essential/regex/pre_char_classes.html
StringTokenizer tokens = new StringTokenizer("Hello I'm your String", " ");
String[] splited = new String[tokens.countTokens()];
int index = 0;
while(tokens.hasMoreTokens()){
splited[index] = tokens.nextToken();
++index;
}
好的,我们需要进行拆分,就像你已经得到了答案,我会进行概括。
如果您想通过空格或定界符(特殊字符)来拆分任何字符串。
首先,删除前导空格,因为它们会引起大部分问题。
str1 = " Hello I'm your String ";
str2 = " Are you serious about this question_ boy, aren't you? ";
首先,删除开头的空格,这些空格可以是空格、制表符等。
String s = str1.replaceAll("^\\s+","");//starting with whitespace one or more
String[] sa = s.split("[^\\w]+");//split by any non word char
但是w包含[a-zA-Z_0-9],所以如果你想通过下划线(_)进行拆分,也可以使用
String[] sa = s.split("[!,? ._'@]+");//for str2 after removing leading space
replaceAll("^\\s+","")
救了我的一天。它对我的情况起作用了。谢谢。 - Kostas Asargiotakis试试这个
String str = "This is String";
String[] splited = str.split("\\s+");
String split_one=splited[0];
String split_second=splited[1];
String split_three=splited[2];
Log.d("Splited String ", "Splited String" + split_one+split_second+split_three);
import java.util.regex.Pattern;
...
private static final Pattern SPACE = Pattern.compile(" ");
String[] arr = SPACE.split(str); // str is the string to be split
这里有它的代码。
String theString="Hello world";
String[] parts = theString.split(" ");
String first = parts[0];//"hello"
String second = parts[1];//"World"