Splitting strings at specific locations in Java
•
Java
Suppose you have a string in the form of "word1 word2 Word3 word4" The easiest way to split it is split [0] = "word1 word2" and split [1] = "Word3 word4"?
Edit: clarification
I want to split, not split [0] = "word1". I have the first two words (I agree it's not clear) and all other words in split [1], that is, in the second space
Solution
I'll use string substring(beginIndex,endIndex); And string substring(beginIndex);
String a = "word1 word2 word3 word4"; int first = a.indexOf(" "); int second = a.indexOf(" ",first + 1); String b = a.substring(0,second); String c = b.subString(second); // Only startindex,cuts at the end of the string
This will result in a = "word1 word2" and B = "Word3 word4"
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码