java:检索字符串中以特定字符串开头和结尾的部分
这就是程序,我有一个字符串java:检索字符串中以特定字符串开头和结尾的部分,java,string,Java,String,这就是程序,我有一个字符串strLineText,我需要从中提取包含target的单词 例如,在字符串“随机字符串与IWANTTHISABC-123及更多”中,我需要提取IWANTTHISABC-123。类似地,如果字符串是“随机字符串带IWANTTHISBBC-001”,我需要提取`IWANTTHISBBC-001。前缀是固定的 我已经用substring()(Method1)尝试过了,但是对于以这个目标词结尾的字符串,逻辑不起作用,即没有输出任何内容 我尝试了split() 您能帮我实现对所
strLineText
,我需要从中提取包含target
的单词
例如,在字符串“随机字符串与IWANTTHISABC-123及更多”
中,我需要提取IWANTTHISABC-123
。类似地,如果字符串是“随机字符串带IWANTTHISBBC-001”
,我需要提取`IWANTTHISBBC-001。前缀是固定的
我已经用substring()
(Method1)尝试过了,但是对于以这个目标词结尾的字符串,逻辑不起作用,即没有输出任何内容
我尝试了split()
您能帮我实现对所有四种组合使用子字符串()
(方法1)吗
public static void main(String[] args) throws IOException {
String target = "IWANTTHIS";
//Four possible inputs
String strLineText = "random string with IWANTTHISABC-123 and more"; //works
String strLineText = "IWANTTHISCBC-45601 and more"; //works
String strLineText = "IWANTTHISEBC-1"; //doesn't work
String strLineText = "random string with IWANTTHISKBC-55545"; //doesn't work
//Method1
System.out.println("O/P 1:" + strLineText.substring(strLineText.indexOf(target),
strLineText.indexOf(target) + strLineText.substring(strLineText.indexOf(target)).indexOf(" ") + 1).trim());
//Method2
for (String s : strLineText.split(" "))
if (s.contains(target))
System.out.println("O/P 2:" + s.trim());
}
strlnetext.substring(strlnetext.indexOf(target)).indexOf(“”)如果strlnetext
在目标字符串后不包含空格,则将为-1。您可以检查strlnetext.substring(strlnetext.indexOf(target))
是否包含空格,如果不包含空格,则将该子字符串保留到strlnetext
的末尾:
//方法1
int beginIndex=strLineText.indexOf(目标);
String substring=strlnetext.substring(beginIndex);
int endIndex=substring.contains(“”)?beginIndex+substring.indexOf(“”):strLineText.length();
System.out.println(“O/P1:+strLineText.substring(beginIndex,endIndex));
我认为这非常简单,您只需要从begin index开始计算end index。以下是适用于所有情况的代码片段
int begin = strLineText.indexOf(target);
int end = strLineText.indexOf(" ", begin);
if(end == -1) end = strLineText.length();
System.out.println(strLineText.substring(begin, end));
假设“word”的定义是字母序列,不包括数字、符号等。对于“word”的其他定义,可以相应地调整正则表达式。如果希望在目标字符串之前包含单词的一部分,可以添加一个从startIndex向后计数的循环,检查字符是否为alpha
public class Foo
{
public static void main(String[] args)
{
String target = "IWANTTHIS";
// String candidate = "random string with IWANTTHISABC-123 and more";
String candidate = "IWANTTHISCBC-45601 and more";
// String candidate = "IWANTTHISEBC-1";
// String candidate = "random string with IWANTTHISKBC-55545";
int startIndex = -1;
int endIndex = -1;
if(candidate.contains(target))
{
System.out.println("Target located.");
startIndex = candidate.indexOf(target);
System.out.println("target starts at " + startIndex);
// keep adding characters until first non-alpha char
endIndex = startIndex + target.length();
boolean wordEnded = false;
while(!wordEnded && (endIndex >= candidate.length()))
{
String foo = Character.toString(candidate.charAt(endIndex + 1));
if(foo.matches("[a-zA-Z]"))
{
endIndex++;
}
else
{
wordEnded = true;
}
}
String full = candidate.substring(startIndex, endIndex + 1);
System.out.println("Full string = " + full);
}
else
{
System.out.println("No target located. Exiting.");
}
}
}
为了使用子字符串
您需要知道开始索引和结束索引,以便从字符串中获得确切的部分您试图提取的“单词”的标准是什么?对于字符串“随机字符串与IWANTTHISABC-123及更多”您想要“IWANTTHISABC”,对吗?意思是找到任何包含目标的连续字母字符序列?字符串“IWANTTHIS”前面是否有字母字符?比如“FOOBARIWANTTHISXYZ”?你能严格描述你想要找到的字符串吗?如果是这样,那么使用java.util.regex包来提取它。