在模式上拆分字符串首先出现,如“;;;”在PostJava中有解释吗?
Split string on pattern coming first like ";;;" Explained in Post Java?
我想断开一个字符串,该字符串最初包含一些不需要的字符并且长度不受限制。
例如:
;;;USA;23;john;;;USA:24;charles
我要的部分在第一个“;;;”之后,即:
USA;23;john;;;USA:24;charles
我试过了:
inputtring1 = ";;;USA;23;john;;;USA:24;charles";
String temp = inputString1.split(";;;")[1];
但是没用;结果显示
USA;23;john
";;;"可能与其他记录一起出现在任意数量的字符串中。上面的字符串是为了表明它不在固定位置。
你可以用老式的方法来做:
int index = string.indexOf(";;;");
string = string.substring(index + 3);
或者使用split
的第二个参数:
string = string.split(";;;", 2)[1];
String Temp=inputString1.split(";;;",1);
将在第一次出现时拆分它。检查一下 String#split
文档
如果您只想要第一个 ";;;"
之后的所有内容,只需这样做:
String stuff = str.replaceFirst(".*?;;;"), "");
这里的一个关键点是 reluctant 量词 *?
,它在匹配的同时尽可能地消耗 little。
string.split(patternString, limit);
正如 String.split(String,int) 解释的那样:
The array returned by this method contains each substring of this
string that is terminated by another substring that matches the given
expression or is terminated by the end of the string. The substrings
in the array are in the order in which they occur in this string. If
the expression does not match any part of the input then the resulting
array has just one element, namely this string.
The limit parameter controls the number of times the pattern is
applied and therefore affects the length of the resulting array. If
the limit n is greater than zero then the pattern will be applied at
most n - 1 times, the array's length will be no greater than n, and
the array's last entry will contain all input beyond the last matched
delimiter.
public static void main(String[] args) {
String inputString = ";;;USA;23;john;;;USA:24;charles";
String temp = inputString.split(";;;", 2)[1];
System.out.println(temp);
}
输出
USA;23;john;;;USA:24;charles
说明
inputString.split(";;;", 2)
// 模式将被应用 (2-1)
次。
如果第一个"wrong character"是任意字符,出现在任意长度,做
String stuff = str.replaceFirst("(.)\1+", "");
我想断开一个字符串,该字符串最初包含一些不需要的字符并且长度不受限制。
例如:
;;;USA;23;john;;;USA:24;charles
我要的部分在第一个“;;;”之后,即:
USA;23;john;;;USA:24;charles
我试过了:
inputtring1 = ";;;USA;23;john;;;USA:24;charles";
String temp = inputString1.split(";;;")[1];
但是没用;结果显示
USA;23;john
";;;"可能与其他记录一起出现在任意数量的字符串中。上面的字符串是为了表明它不在固定位置。
你可以用老式的方法来做:
int index = string.indexOf(";;;");
string = string.substring(index + 3);
或者使用split
的第二个参数:
string = string.split(";;;", 2)[1];
String Temp=inputString1.split(";;;",1);
将在第一次出现时拆分它。检查一下 String#split
文档
如果您只想要第一个 ";;;"
之后的所有内容,只需这样做:
String stuff = str.replaceFirst(".*?;;;"), "");
这里的一个关键点是 reluctant 量词 *?
,它在匹配的同时尽可能地消耗 little。
string.split(patternString, limit);
正如 String.split(String,int) 解释的那样:
The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string. The substrings in the array are in the order in which they occur in this string. If the expression does not match any part of the input then the resulting array has just one element, namely this string.
The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter.
public static void main(String[] args) {
String inputString = ";;;USA;23;john;;;USA:24;charles";
String temp = inputString.split(";;;", 2)[1];
System.out.println(temp);
}
输出
USA;23;john;;;USA:24;charles
说明
inputString.split(";;;", 2)
// 模式将被应用 (2-1)
次。
如果第一个"wrong character"是任意字符,出现在任意长度,做
String stuff = str.replaceFirst("(.)\1+", "");