-1

I want to get a specific set of characters after a substring:

My substring is:

"Element:Neo"

The set of characters I want are : Neo

Solutions I have tried:

String descriptions = "Element:Neo";
String checker = descriptions.substring(descriptions.indexOf("Element:")+1);
System.out.println(checker);

but I got my print statement as: lement:Neo, instead of the desired Neo

So is it possible to use indexof and get just Neo

Links I followed: how to find before and after sub-string in a string - but the given solution, didn't help me, hence I asked the question.

tyzion
  • 260
  • 1
  • 10
  • 1
    When you say "after a specific substring", your substring is 'Element:'" - "Element:Neo" is the whole string you want to retain a substring of. – daniu Mar 23 '23 at 07:57
  • No element neo is part of a larger substring – tyzion Mar 23 '23 at 07:58
  • `indexOf` returns the *start* of the string it matches – tgdavies Mar 23 '23 at 08:00
  • So your actual String is something like "Some String Element:Neo", and you want to extract Neo. – daniu Mar 23 '23 at 08:07
  • `Neo` is not **after** the substring, it is **part of** your substring. How do you determine you want `Neo`, is it because it is comes after `:`, or something else? – Mark Rotteveel Mar 23 '23 at 09:30
  • api doc is your friend, best to study that of whatever method you are using :) Here that would be: https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html#indexOf(java.lang.String) – kleopatra Mar 23 '23 at 10:14

2 Answers2

1

You can do

String input = "This is a line containing Element:Neo";
String[] split = input.split("Element:");
String result = (split.length > 1)
  ? result = split[1]  // match found
  : ""                 // some default
}

Or, if you want to overengineer things,

Pattern pattern = Pattern.compile(".*Element:(.*)");
String input = "This is a line containing Element:Neo";
Matcher matcher = pattern.matcher(input);
String result = matcher.groupCount() > 0
  ? matcher.group(1)
  : "";  
}
daniu
  • 14,137
  • 4
  • 32
  • 53
0

If you have only one occurence of required element then you can use indexOf and substring:

String baseString = "Element:";
String checker = descriptions.indexOf(baseString) < 0
  ? ""
  : descriptions
      .substring(descriptions.indexOf(baseString)
        + baseString.length());

But if you have multiple occurences of required elements then you can use String.split:

String descriptions = "Some Element not needed Element:NeoElement:AgentElement:TrinityElement:Oracle";
String baseString = "Element:";
List<String> checker;
int firstIndex = descriptions.indexOf(baseString);
if (firstIndex >= 0){
  // remove unwanted first element
  checker = Arrays.stream(descriptions.split(baseString))
    .skip(1).collect(Collectors.toList());
} else {
  checker = Collections.emptyList();
}
System.out.println(checker);
Aqeel Ashiq
  • 1,988
  • 5
  • 24
  • 57
  • 1
    The indexOf would only be needed if it's a part of a longer string, which OP did not say it would be. Also the length() would provide the proper position alone so the +1 is't required. – Randommm Mar 23 '23 at 07:47
  • 1
    @Randommm where does OP state that it's not part of a longer string? – daniu Mar 23 '23 at 07:55
  • @Randommm OP never stated that it would not be part of longer string. Also I removed the +1, thanks for pointing out that. I also updated the answer for handling multiple scenarios – Aqeel Ashiq Mar 23 '23 at 08:29
  • @daniu Well then they would need help with figuring out how long they need to make the substring too. – Randommm Mar 23 '23 at 08:42