Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract different substrings between certain substrings from array list?

I'm not sure how to properly ask this question, but hopefully this example will clear things up.

I have a list of Strings, for example ("AS3K4455NJNSADN4455KLMDSMF043J4455NKFDG4455KMMFDG")

As you can see, there is "4455" repeating. I'll have a long list of stings and let's say 4455 will represent start/end of message I want to extract. I'm not what is the best way to achieve this, so if anyone ever encoutered similar problem, please halp :)

like image 901
joe Avatar asked Jun 18 '26 16:06

joe


1 Answers

You can split them by using a StringTokenizer, and setting your delim as you prefer (in your example, it's 4455), ie:

String string = "AS3K4455NJNSADN4455KLMDSMF043J4455NKFDG4455KMMFDG";
StringTokenizer tokenizer = new StringTokenizer(string, "4455");
while(tokenizer.hasMoreTokens()){
    System.out.println("Token: "+ tokenizer.nextToken()); //or do whatever you need with your token, here I'm printing
}

Output:

Token: AS3K
Token: NJNSADN
Token: KLMDSMF0
Token: 3J
Token: NKFDG
Token: KMMFDG
like image 199
Leviand Avatar answered Jun 20 '26 07:06

Leviand



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!