[英]Using Java subString to format a string separated by dot (“.”)
I have string like this: 我有这样的字符串:
com.company.foobar.message
And the other input to function is another string "foobar". 函数的另一个输入是另一个字符串“ foobar”。
Now, I need to format the string in such a way that I will return foobar.message as the resulting string. 现在,我需要格式化字符串,以便将foobar.message作为结果字符串返回。 The position of foobar in the original string is not fixed.
foobar在原始字符串中的位置不固定。
Just find the location of foobar, and take the rest of the string. 只需找到foobar的位置,然后取出字符串的其余部分即可。
int index = str.indexOf("foobar");
if (index != -1) {
return str.substring(index);
} else {
// not found - do something
}
You can do it in this way assuming that you need everything after that foobar 假设您在foobar之后需要一切,就可以用这种方式进行操作
package com;
public class Test {
public static void main(String[] args) {
String string = "com.company.foobar.message";
String returnValue=Checkfor(string,"foobar");
System.out.println(returnValue);
}
private static String Checkfor(String string, String string2) {
int start=string.indexOf(string2);
if (start!=-1){
return string.substring(start);
}
else
// TODO Auto-generated method stub
return null;
}
}
OUTPUT foobar.message
输出
foobar.message
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.