-2

I am trying to only display data after a certain static word (in)

Example:

String jobName = job.getDescription();

returns the following:

XYZ/LMNOP in ABCEFG

I only want the data after the "in" in this scenario. However the XYZ/LMNOP is different in almost every case so I cannot simply call out that section of the string.

iairj84
  • 1
  • 1
  • 1

4 Answers4

1

You can use split() in the String class.

String jobName = job.getDescription();
String[] parts = jobName.split("in"); { "XYZ/LMNOP", "ABCEFG" }
String before = parts[0]; // XYZ/LMNOP 
String after = parts[1]; // ABCEFG
Rossiar
  • 2,416
  • 2
  • 23
  • 32
0

Find index of "in" in the string and then use the string from that particular index+3 to last.

int k = p.indexOf("in");
System.out.println(p.substring(k+3));

index+3 because "i", "n" , " " are 3 characters.

Priyansh Goel
  • 2,660
  • 1
  • 13
  • 37
0

try using this

String search = " in ";
String d = job.getDescription();
d = d.substring(d.indexOf(search) + search.length(), d.length());

Outputs, given the inputs:

[find something in a string] -> [a string]
[finding something in a string] -> [a string] // note findINg, that does not match

The search key can be changed to simply in if desired, or left as is to match the question posted to avoid an accidental in in a word.

If you so choose, you can also use .toLower() on getDescription() if you want to be case insensitive when matching the word in as well.

Matt Clark
  • 27,671
  • 19
  • 68
  • 123
0

First you need to understand your strings possible data values. If it is always <some_text> in <some_text> then there are muliple ways as other users have mentioned.

Here is another way, whih is bit simpler

String[] strArray = job.getDescription().split(" "); //splits array on spaces
System.out.println(strArray[2]);
Em Ae
  • 8,167
  • 27
  • 95
  • 162