public class SplitStringobj {
public static void main(String[] args) {
String val1= Start//complete//First//com//upload the dummy123//First//download;
String val2= First;
String[] splitObject = outObject.split("//");
for(String obj :splitObject) {
if(outObject.startsWith(obj.toString());
break;
}
}
}我需要在交货期以下
字符串val1=开始//完成//第一/com//上载虚拟文件123//First//下载
字符串val2=First
Output=First//com//上载dummy123//First//download
字符串val1=开始//完成//第一/com//上载虚拟文件123//First//下载
字符串val2=complete
输出=完成//First//com//上载dummy123//First//download
字符串val1=开始//完成//第一/com//上载虚拟文件123//First//下载
字符串val2=com
Output=com//上载dummy123//First//download
发布于 2022-05-18 14:39:06
这是我的尝试。
public static String first_match(String[] str, String toMatch) {
boolean match = false;
StringBuilder output = new StringBuilder();
for (int i=0; i<str.length; i++) {
if (str[i].equals(toMatch)) {
match = true;
}
if (match){
output.append(str[i]);
if (i != str.length-1) {
output.append("//");
}
}
}
return output.toString();
}使用上述方法用于:
String val1 = "Start//complete//First//com//upload//First//download";
String val2 = "First";
String[] splitObject = val1.split("//");
String out = first_match(splitObject, val2);
System.out.println(out);它提供了产出:
First//com//upload//First//download编辑:
我刚刚从评论中意识到,可以通过以下方式更容易做到这一点:
public static String firstMatch(String str, String toMatch) {
int index = str.indexOf(toMatch);
if (index == -1) return "";
return str.substring(index);
}
String out1 = firstMatch(val1, val2);编辑2:
这是另一个一条龙的方法。
val1.replaceFirst(".*?" + val2, val2)https://stackoverflow.com/questions/72290905
复制相似问题