简体   繁体   English

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

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

This is the program and i have a string strLineText from which i need to extract the words that contain target in them.这是程序,我有一个字符串strLineText ,我需要从中提取包含target的单词。

Ex.前任。 In the string "random string with IWANTTHISABC-123 and more" i need to extract IWANTTHISABC-123 .在字符串"random string with IWANTTHISABC-123 and more"我需要提取IWANTTHISABC-123 Similarly if the string is "random string with IWANTTHISBBC-001" i need to extract `IWANTTHISBBC-001.同样,如果字符串是"random string with IWANTTHISBBC-001"我需要提取 `IWANTTHISBBC-001。 The prefix is fixed前缀是固定的

I have tried it with substring() (Method1) but the logic doesn't work for Strings that end with this target word ie, nothing is outputted我已经用substring() (Method1)尝试过,但逻辑不适用于以这个目标词结尾的字符串,即,没有输出任何内容

I tried the split() (Method2) and it works for all four combinations.我尝试了split() (Method2)并且它适用于所有四种组合。

Can you help me with achieving using the substring() (Method1) for all four combinations你能帮我实现对所有四种组合使用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(" ") will be -1 if strLineText contains no spaces after your target string. strLineText.substring(strLineText.indexOf(target)).indexOf(" ")如果strLineText在目标字符串后不包含空格, strLineText -1。 You could check if strLineText.substring(strLineText.indexOf(target)) contains spaces, and if not, take the substring until the end of strLineText :您可以检查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));

I think it is pretty straightforward, you just need to compute the end index starting from begin index.我认为这很简单,您只需要从开始索引开始计算结束索引。 Here is the snippet that works for all cases.这是适用于所有情况的代码段。

int begin = strLineText.indexOf(target);
int end = strLineText.indexOf(" ", begin);
if(end == -1) end = strLineText.length();

System.out.println(strLineText.substring(begin, end));

Assumes that your definition of "word" is a sequence of alphas, excluding numbers, symbols, etc. For other definitions of "word," the regular expression can be adjusted accordingly.假设您对“word”的定义是一个字母序列,不包括数字、符号等。对于“word”的其他定义,可以相应地调整正则表达式。 If you want to include parts of the word previous to the target string, you can add a loop that counts backwards from startIndex, examining characters to see if they are alpha.如果您想在目标字符串之前包含单词的一部分,您可以添加一个从 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