Say I have a string with various words of unknown length. I plan to split the string by using a regular expression. Something lik开发者_C百科e:
String resString = origString.split(".*//s.*//s")[0];
What would be the regular expression to get the first two words? I was thinking .*//s.*//s
, so all characters, followed by a space, then all characters, followed by another space. But using that gives me the exact same string I had before. Am I going about this the wrong way?
Any help would be appreciated!
If you have only spaces between words, split by \\s+
. When you split, the array would be the words themselves. First two would be in arr[0]
and arr[1]
if you do:
String[] arr = origString.split("\\s+");
With regular expressions you can do something like this:
public static ArrayList<String> split2(String line, int n){
line+=" ";
Pattern pattern = Pattern.compile("\\w*\\s");
Matcher matcher = pattern.matcher(line);
ArrayList<String> list = new ArrayList<String>();
int i = 0;
while (matcher.find()){
if(i!=n)
list.add(matcher.group());
else
break;
i++;
}
return list;
}
if you want the first n words, or simply this:
public static String split3(String line){
line+=" ";
Pattern pattern = Pattern.compile("\\w*\\s\\w*\\s");
Matcher matcher = pattern.matcher(line);
matcher.find();
return matcher.group();
}
if you want only the first and second words.
If you want to split it on exactly the space character:
String[] parts = args[i].split(" ");
If you want to split it on any whitespace character (space, tab, newline, cr):
String[] parts = args[i].split("\\s");
To treat multiple adjacent spaces as one separator:
String[] parts = args[i].split(" +");
Same for whitespace:
String[] parts = args[i].split("\\s+");
The first two words would be parts[0]
and parts[1]
Assuming your "words" consist of alphanumeric characters, the following regex will match the first 2 words:
\w+\s+\w+
Use below method.
public static String firstLettersOfWord(String word) {
StringBuilder result = new StringBuilder();
String[] myName = word.split("\\s+");
for (String s : myName) {
result.append(s.charAt(0));
}
return result.toString();
}}
精彩评论