I have a string say 123dance456
which I need to split into two strings containing the first sub-string before the sub-string dance
(i.e. 123
) and after the sub-string dance
(i.e. 456
). I need to find them and hold them in separate string variables, say String firstSubString = 123;
and String secondSubString = 456;
.
我有一根弦,比如123dance456,我需要把它分成两根弦,分别包含子弦舞(即123)和子弦舞(即456)之前的第一个子弦。我需要找到它们并把它们放在单独的字符串变量中,比如字符串firstSubString = 123;字符串secondSubString = 456;
Is there any given String method that does just that?
有给定的字符串方法可以做到这一点吗?
6 个解决方案
#1
44
You can use String.split(String regex)
. Just do something like this:
您可以使用字符串。分裂(字符串正则表达式)。就像这样:
String s = "123dance456";
String[] split = s.split("dance");
String firstSubString = split[0];
String secondSubString = split[1];
Please note that if "dance"
occurs more than once in the original string, split()
will split on each occurrence -- that's why the return value is an array.
请注意,如果“dance”在原始字符串中出现的次数超过一次,那么split()将对每个事件进行分割——这就是为什么返回值是一个数组。
#2
17
You can do this:
你可以这样做:
String str = "123dance456";
String substr = "dance";
String before = str.substring(0, str.indexOf(substr));
String after = str.substring(str.indexOf(substr) + substr.length());
Or
或
String str = "123dance456";
String substr = "dance";
String[] parts = str.split(substr);
String before = parts[0];
String after = parts[1];
It is noteworthy that the second answer not work if we have more than one occurrence of the substring. To that end, if we only want the first one to be recognized, it would be safer to call split with a limit:
值得注意的是,如果子字符串的出现次数不止一次,第二个答案就不能工作。为此目的,如果我们只希望第一个得到承认,那么就更安全地对split进行限定:
String[] parts = str.split(substr, 2);
which ensures that the returned array has at most two elements. Also, since split
will interpret its input as a regular expression we have to be wary of invalid regular expression syntax. As such, I would much rather the first solution, since it works irrespective of the composition of the original substring.
这确保返回的数组最多有两个元素。而且,由于split将把它的输入解释为正则表达式,因此我们必须警惕无效的正则表达式语法。因此,我宁愿选择第一个解决方案,因为它与原始子字符串的组合无关。
To make the first answer more efficient -- as it is my preferred answer -- then, we would need to remember the position of the substring:
为了使第一个答案更有效——因为这是我的首选答案——那么,我们需要记住子串的位置:
final int position = str.indexOf(substr);
if (position >= 0) {
//if the substring does occur within the string, set the values accordingly
before = str.substring(0, position);
after = str.substring(position + substr.length());
} else {
//otherwise, default to the empty string (or some other value)
before = "";
after = "";
}
It always pays to pay attention to these little edge cases.
注意这些小的边界情况总是值得的。
#3
4
Easiest is to use the split
method as the other answers suggest. You can also use a Matcher
and a Pattern
for a more general approach:
最简单的方法是按照其他答案的建议使用split方法。您还可以使用一个Matcher和一个模式来实现更一般的方法:
String str = "123dance456";
String splitter = "dance";
Pattern p = Pattern.compile("(.*?)" + splitter + "(.*)");
Matcher m = p.matcher(str);
if (m.matches()) {
firstSubString = m.group(1); // may be empty
secondSubString = m.group(2); // may be empty
} else {
// splitter not found in str
}
#4
4
If you are using commons-lang see StringUtils.substringAfter()
如果您正在使用common -lang,请参阅StringUtils.substringAfter()
#5
1
String[] data = new String("123dance456").split("dance");
#6
0
Using the Scanner is a nice alternative:
使用扫描仪是一个不错的选择:
Scanner scanner = new Scanner( "123dance456" ).useDelimiter( "dance" );
if ( scanner.hasNext() )
System.out.println( scanner.next() );
if ( scanner.hasNext() )
System.out.println( scanner.next() );
It is quite convenient to process the tokens as a stream and simply ask via hasNext()
if new tokens are available. Also you get nice conversions from the Scanner
, even localised, like:
将令牌作为流处理,并简单地通过hasNext()询问是否有新的令牌可用,这非常方便。你还可以从扫描仪中得到很好的转换,甚至是局部的转换,比如:
Scanner scanner = new Scanner( "123dance456" ).useDelimiter( "dance" );
System.out.println( scanner.nextDouble() * scanner.nextDouble() );
#1
44
You can use String.split(String regex)
. Just do something like this:
您可以使用字符串。分裂(字符串正则表达式)。就像这样:
String s = "123dance456";
String[] split = s.split("dance");
String firstSubString = split[0];
String secondSubString = split[1];
Please note that if "dance"
occurs more than once in the original string, split()
will split on each occurrence -- that's why the return value is an array.
请注意,如果“dance”在原始字符串中出现的次数超过一次,那么split()将对每个事件进行分割——这就是为什么返回值是一个数组。
#2
17
You can do this:
你可以这样做:
String str = "123dance456";
String substr = "dance";
String before = str.substring(0, str.indexOf(substr));
String after = str.substring(str.indexOf(substr) + substr.length());
Or
或
String str = "123dance456";
String substr = "dance";
String[] parts = str.split(substr);
String before = parts[0];
String after = parts[1];
It is noteworthy that the second answer not work if we have more than one occurrence of the substring. To that end, if we only want the first one to be recognized, it would be safer to call split with a limit:
值得注意的是,如果子字符串的出现次数不止一次,第二个答案就不能工作。为此目的,如果我们只希望第一个得到承认,那么就更安全地对split进行限定:
String[] parts = str.split(substr, 2);
which ensures that the returned array has at most two elements. Also, since split
will interpret its input as a regular expression we have to be wary of invalid regular expression syntax. As such, I would much rather the first solution, since it works irrespective of the composition of the original substring.
这确保返回的数组最多有两个元素。而且,由于split将把它的输入解释为正则表达式,因此我们必须警惕无效的正则表达式语法。因此,我宁愿选择第一个解决方案,因为它与原始子字符串的组合无关。
To make the first answer more efficient -- as it is my preferred answer -- then, we would need to remember the position of the substring:
为了使第一个答案更有效——因为这是我的首选答案——那么,我们需要记住子串的位置:
final int position = str.indexOf(substr);
if (position >= 0) {
//if the substring does occur within the string, set the values accordingly
before = str.substring(0, position);
after = str.substring(position + substr.length());
} else {
//otherwise, default to the empty string (or some other value)
before = "";
after = "";
}
It always pays to pay attention to these little edge cases.
注意这些小的边界情况总是值得的。
#3
4
Easiest is to use the split
method as the other answers suggest. You can also use a Matcher
and a Pattern
for a more general approach:
最简单的方法是按照其他答案的建议使用split方法。您还可以使用一个Matcher和一个模式来实现更一般的方法:
String str = "123dance456";
String splitter = "dance";
Pattern p = Pattern.compile("(.*?)" + splitter + "(.*)");
Matcher m = p.matcher(str);
if (m.matches()) {
firstSubString = m.group(1); // may be empty
secondSubString = m.group(2); // may be empty
} else {
// splitter not found in str
}
#4
4
If you are using commons-lang see StringUtils.substringAfter()
如果您正在使用common -lang,请参阅StringUtils.substringAfter()
#5
1
String[] data = new String("123dance456").split("dance");
#6
0
Using the Scanner is a nice alternative:
使用扫描仪是一个不错的选择:
Scanner scanner = new Scanner( "123dance456" ).useDelimiter( "dance" );
if ( scanner.hasNext() )
System.out.println( scanner.next() );
if ( scanner.hasNext() )
System.out.println( scanner.next() );
It is quite convenient to process the tokens as a stream and simply ask via hasNext()
if new tokens are available. Also you get nice conversions from the Scanner
, even localised, like:
将令牌作为流处理,并简单地通过hasNext()询问是否有新的令牌可用,这非常方便。你还可以从扫描仪中得到很好的转换,甚至是局部的转换,比如:
Scanner scanner = new Scanner( "123dance456" ).useDelimiter( "dance" );
System.out.println( scanner.nextDouble() * scanner.nextDouble() );