user1755178
user1755178

Reputation: 253

How to add delimiters from the StringTokenizers to a seperate string?

I am inputting a string and I want to add the delimeters in that string to a different string and I was wondering how you would do that. This is the code I have at the moment.

StringTokenizer tokenizer = new StringTokenizer(input, "'.,><-=[]{}+!@#$%^&*()~`;/?");
while (tokenizer.hasMoreTokens()){
      //add delimeters to string here
}

Any help would be greatly appreciated(:

Upvotes: 0

Views: 204

Answers (4)

Shiva Komuravelly
Shiva Komuravelly

Reputation: 3280

This would even take care of repeated usage of delimeters

String input = "adfhkla.asijdf.';.akjsdhfkjsda";
String compDelims = "'.,><-=[]{}+!@#$%^&*()~`;/?";
String delimsUsed = "";
for (char a : compDelims.toCharArray()) {
    if (input.indexOf(a) > 0 && delimsUsed.indexOf(a) == -1) {
         delimsUsed += a;
    }
}
System.out.println("The delims used are " + delimsUsed);

Upvotes: 1

Aditya
Aditya

Reputation: 2246

I'm guessing you want to extract all the delimiters from the string and process them

String allTokens = "'.,><-=[]{}+!@#$%^&*()~`;/?";
StringTokenizer tokenizer = new StringTokenizer(input, allTokens, true);
while(tokenizer.hasMoreTokens()) {
    String nextToken = tokenizer.nextToken();
    if(nextToken.length()==1 && allTokens.contains(nextToken)) {
        //this token is a delimiter
        //append to string or whatever you want to do with the delimiter
        processDelimiter(nextToken);
    }
}

Create a processDelimiter method in which you add the delimiter to a different string or perform any action you want.

Upvotes: 1

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

I don't think StringTokenizer is good for this task, try

    StringBuilder sb = new StringBuilder();
    for(char c : input.toCharArray()) {
        if ("'.,><-=[]{}+!@#$%^&*()~`;/?".indexOf(c) >= 0) {
            sb.append(c);
        }
    }

Upvotes: 1

Karthik T
Karthik T

Reputation: 31952

If you want StringTokenizer to return the delimiters it parses, you would need to add a flag to the constructor as shown here

StringTokenizer tokenizer = new StringTokenizer(input, "'.,><-=[]{}+!@#$%^&*()~`;/?", true);

But if you are searching only for delimiters I dont think this is the right approach.

Upvotes: 1

Related Questions