0

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(:

4
  • 1
    you mean you want to use any of this characters as delimiter? Commented Jan 4, 2013 at 5:04
  • 1
    question is not clear; can u give some example on your expected output Commented Jan 4, 2013 at 5:06
  • yes, I want to use all of those punctuation marks as delimiters. An example of my output would be a string that is "!!@#.." if input = "2!3!ab@#gh.hhhh." Commented Jan 4, 2013 at 5:09
  • 1
    answer mentioned below by Karthik T should hold good. Commented Jan 4, 2013 at 5:11

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

If I add a flag, then how do I ask it to return the delimiter? And I am searching for both delimiters and the letters that are not delimiters in the string
1

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); } } 

2 Comments

I've never used StringBuilder before...can you construct one that is of unknown size? Since the initial capacity is 16 characters, can it hold more than that if you keep adding characters to the string?
You can, eg new StringBuilder(32), but in our case the length is not known. We should not worry about it, StringBuilder will increase capacity when need be. And finally you make a String of it as sb.toString();
1

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.

1 Comment

If the previous answer does not work for me, I will try this out. Thank you!
1

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); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.