Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture delimiters also as tokens in result

Tags:

java

Below is the code for creating tokens based on delimiters. This will give the tokens but will not consider delimiters in token result. If we want to include the delimiters also in result, what should be the approach.

StringTokenizer tr=
new StringTokenizer("Will you come, yes/no",",/");
while (tr.hasMoreElements( ))
System.out.println(tr.nextElement( )+",");

So output should be tokens as:

Will, ,you, ,come, ,yes,/,no,

(Instead of just will,you,come,yes,no,)

like image 567
Aajan Avatar asked Dec 02 '25 09:12

Aajan


1 Answers

/ is not coming as one of the tokens because it is not surrounded by the delimiter. If you want it to be parsed as one of the tokens then you can surround it with delimiters.

Here is the code snippet:

public static void main (String[] args)
{
    String sample = "Will you come,yes/no";
    StringTokenizer tr = new StringTokenizer(sample.replace(" ",",").replace("/",",/,"),",");
    while (tr.hasMoreElements())
        System.out.println(tr.nextElement() + ",");
}

Output:

Will,
you,
come,
yes,
/,
no,
like image 145
user2004685 Avatar answered Dec 03 '25 22:12

user2004685



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!