i have a string in Java.
我有一个Java字符串。
1|2|3|4|5|2|2|3|4|123441|234556|67783|56764|55454
i want to count a delimiter above string. Please help me in how to count and i want only starting 7 Delimiter value.
我想计算一个字符串以上的分隔符。请帮助我如何计算,我只想启动7 Delimiter值。
3 个解决方案
#1
1
You can split using |
character.
您可以使用|进行拆分字符。
public static void main(String[] args) {
String s = "1|2|3|4|5|2|2|3|4|123441|234556|67783|56764|55454";
String[] strArr = s.split("\\|");
System.out.println("Array : " + Arrays.toString(strArr));
System.out.println("Delimiter count : " + (strArr.length - 1)); // Prints 13
System.out.println("7th field : " + strArr[7]); // Prints 3
}
#2
1
You can solve it with regular expressions, using Pattern and Matcher classes:
您可以使用Pattern和Matcher类使用正则表达式解决它:
String s = "1|2|3|4|5|2|2|3|4|123441|234556|67783|56764|55454";
Pattern p = Pattern.compile("((\\d+\\|){7}).*");
Matcher m = p.matcher(s);
if (m.matches()) {
System.out.println(m.group(1));
}
To understand the code above, have a look at regular expressions, e.g. http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
要理解上面的代码,请查看正则表达式,例如: http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
#3
1
In case if your input string if sthg like this:-
万一你的输入字符串如果sthg如下: -
1|2|3|4|5|2|2|3|4|123441|234556|||
having empty values between delimiters. Then you can go with a different version of split
function.
在分隔符之间具有空值。然后你可以使用不同版本的分割功能。
String[] strArr = s.split("\\|", -1);
You need to pass -1 as the second argument to split otherwise it removes empty strings.
你需要传递-1作为第二个参数,否则它将删除空字符串。
#1
1
You can split using |
character.
您可以使用|进行拆分字符。
public static void main(String[] args) {
String s = "1|2|3|4|5|2|2|3|4|123441|234556|67783|56764|55454";
String[] strArr = s.split("\\|");
System.out.println("Array : " + Arrays.toString(strArr));
System.out.println("Delimiter count : " + (strArr.length - 1)); // Prints 13
System.out.println("7th field : " + strArr[7]); // Prints 3
}
#2
1
You can solve it with regular expressions, using Pattern and Matcher classes:
您可以使用Pattern和Matcher类使用正则表达式解决它:
String s = "1|2|3|4|5|2|2|3|4|123441|234556|67783|56764|55454";
Pattern p = Pattern.compile("((\\d+\\|){7}).*");
Matcher m = p.matcher(s);
if (m.matches()) {
System.out.println(m.group(1));
}
To understand the code above, have a look at regular expressions, e.g. http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
要理解上面的代码,请查看正则表达式,例如: http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
#3
1
In case if your input string if sthg like this:-
万一你的输入字符串如果sthg如下: -
1|2|3|4|5|2|2|3|4|123441|234556|||
having empty values between delimiters. Then you can go with a different version of split
function.
在分隔符之间具有空值。然后你可以使用不同版本的分割功能。
String[] strArr = s.split("\\|", -1);
You need to pass -1 as the second argument to split otherwise it removes empty strings.
你需要传递-1作为第二个参数,否则它将删除空字符串。