基于Java中字符串的第n次出现拆分字符串
如何基于分隔符的第n次出现(例如:秒)拆分字符串。除第n次出现外,应保留所有其他分隔符 I/p: O/p: 我是这样得到的基于Java中字符串的第n次出现拆分字符串,java,regex,string,Java,Regex,String,如何基于分隔符的第n次出现(例如:秒)拆分字符串。除第n次出现外,应保留所有其他分隔符 I/p: O/p: 我是这样得到的 String name="This is my First Line"; int count=3; String s1,s2; String arr[]=name.split();//default will be space for(i=0;i<arr.length;i++) if(i<count) s1=s1+arr[i]+
String name="This is my First Line";
int count=3;
String s1,s2;
String arr[]=name.split();//default will be space
for(i=0;i<arr.length;i++)
if(i<count)
s1=s1+arr[i]+" "
else
s2=s2+arr[i]+" "
String name=“这是我的第一行”;
整数计数=3;
字符串s1、s2;
字符串arr[]=name.split()//默认值为空格
对于(i=0;i只需使用indexOf
搜索分隔符,并重复该操作,直到找到它为止。下面是一个片段:
String name = "This is my First Line";
String delimiter = " ";
int count = 3;
// Repeativly search for the delimiter
int lastIndex = -1;
for (int i = 0; i < count; i++) {
// Begin to search from the position after the last matching index
lastIndex = name.indexOf(delimiter, lastIndex + 1);
// Could not be found
if (lastIndex == -1) {
break;
}
}
// Get the result
if (lastIndex == -1) {
System.out.println("Not found!");
} else {
// Use the index to split
String before = name.substring(0, lastIndex);
String after = name.substring(lastIndex);
// Print the results
System.out.println(before);
System.out.println(after);
}
请注意最后一行开头的空格(分隔符),如果需要,可以在结尾使用以下代码忽略此空格
// Remove the delimiter from the beginning of 'after'
String after = ...
after = after.subString(delimiter.length());
由于正则表达式的限制,您不能将其拆分为一行代码,但可以分为两行:
String firstPart = name.replaceAll("^((.*?" + delimiter + "){" + count + "}).*", "$1");
String secondPart = name.replaceAll("^(.*?" + delimiter + "){" + count + "}(.*)", "$2");
静态类FindNthOccurrence
{
字符串分隔符;
公共查找不发生(字符串删除)
{
this.delimiter=del;
}
公共int findAfter(字符串findIn,int findOccurence)
{
int findIndex=0;
for(int i=0;i
就像这样,
在Java8中测试并完美工作
public String[] split(String input,int at){
String[] out = new String[2];
String p = String.format("((?:[^/]*/){%s}[^/]*)/(.*)",at);
Pattern pat = Pattern.compile(p);
Matcher matcher = pat.matcher(input);
if (matcher.matches()) {
out[0] = matcher.group(1);// left
out[1] = matcher.group(2);// right
}
return out;
}
//Ex: D:/folder1/folder2/folder3/file1.txt
//if at = 2, group(1) = D:/folder1/folder2 and group(2) = folder3/file1.txt
请查看String
类中的split(String,int)
方法。@Thomas在我的例子中,它将字符串分成两部分String 1=“This”,String2=“是我的第一行”啊,现在我明白你的问题了。你想在第三个空格上拆分。在这种情况下,动态构建一个正则表达式。有很多关于表达式应该是什么样子的示例。或者使用indexOf()
和substring()的组合
。查看可能的重复项。我们需要转换为数组,然后追加它,有没有更简单的方法。因为我的字符串可能类似于“this is my hello world”,带有多个空格,所以我需要保留这些空格。请使用trim()以避免在开始和结束处出现空格。如果中间有更多空格,请使用split(\\s*)我的评论没有传达我的问题,现在我修改了我的问题。请注意,分隔符将被保留。分配给新的变量好的解决方案,如果您使用模式,效果会更好。引号(分隔符)
如果字符串包含点,例如This.is.my.First.Line
:)为什么滥发重复答案?您还对多个问题执行了此操作。
// Remove the delimiter from the beginning of 'after'
String after = ...
after = after.subString(delimiter.length());
String firstPart = name.replaceAll("^((.*?" + delimiter + "){" + count + "}).*", "$1");
String secondPart = name.replaceAll("^(.*?" + delimiter + "){" + count + "}(.*)", "$2");
static class FindNthOccurrence
{
String delimiter;
public FindNthOccurrence(String del)
{
this.delimiter = del;
}
public int findAfter(String findIn, int findOccurence)
{
int findIndex = 0;
for (int i = 0; i < findOccurence; i++)
{
findIndex = findIn.indexOf(delimiter, findIndex);
if (findIndex == -1)
{
return -1;
}
}
return findIndex;
}
}
FindNthOccurrence nth = new FindNthOccurrence(" ");
String string = "This is my First Line";
int index = nth.nthOccurrence(string, 2);
String firstPart = string.substring(0, index);
String secondPart = string.substring(index+1);
public String[] split(String input,int at){
String[] out = new String[2];
String p = String.format("((?:[^/]*/){%s}[^/]*)/(.*)",at);
Pattern pat = Pattern.compile(p);
Matcher matcher = pat.matcher(input);
if (matcher.matches()) {
out[0] = matcher.group(1);// left
out[1] = matcher.group(2);// right
}
return out;
}
//Ex: D:/folder1/folder2/folder3/file1.txt
//if at = 2, group(1) = D:/folder1/folder2 and group(2) = folder3/file1.txt