繁体   English   中英

java:检索以特定字符串开头和结尾的字符串的一部分

[英]java: to retrieve a part of a string that begins and ends with a specific string

这是程序,我有一个字符串strLineText ,我需要从中提取包含target的单词。

前任。 在字符串"random string with IWANTTHISABC-123 and more"我需要提取IWANTTHISABC-123 同样,如果字符串是"random string with IWANTTHISBBC-001"我需要提取 `IWANTTHISBBC-001。 前缀是固定的

我已经用substring() (Method1)尝试过,但逻辑不适用于以这个目标词结尾的字符串,即,没有输出任何内容

我尝试了split() (Method2)并且它适用于所有四种组合。

你能帮我实现对所有四种组合使用substring() (Method1)

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());
}

strLineText.substring(strLineText.indexOf(target)).indexOf(" ")如果strLineText在目标字符串后不包含空格, strLineText -1。 您可以检查strLineText.substring(strLineText.indexOf(target))包含空格,如果没有,则取子字符串直到strLineText结束:

//Method1
int beginIndex = strLineText.indexOf(target);
String substring = strLineText.substring(beginIndex);
int endIndex = substring.contains(" ") ? beginIndex + substring.indexOf(" ") : strLineText.length();
System.out.println("O/P 1:" + strLineText.substring(beginIndex, endIndex));

我认为这很简单,您只需要从开始索引开始计算结束索引。 这是适用于所有情况的代码段。

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.");
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM