GeeCON Prague 2014*
The moose likes Java in General and the fly likes Remove extra space from a String without split(),trim() and stringTokenizer() method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Remove extra space from a String without split(),trim() and stringTokenizer() method?" Watch "Remove extra space from a String without split(),trim() and stringTokenizer() method?" New topic
Author

Remove extra space from a String without split(),trim() and stringTokenizer() method?

Rd Dari
Ranch Hand

Joined: Feb 22, 2010
Posts: 211

hi all ,

I have a technical program during an interview...........
Write a program to remove extra spaces of the String without split(),trim() and tokenizer() method?



I am not able to find it on web so please give me any idea for it.

Thanks in advanced!!

[EFH: Added code tags so spacing is preserved]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39079
    
  23
I can’t see any difference! You might find it looks better if I add code tags, because the forum software does not support double whitespace. And you should not use StringTokenizer at all, as you will find it you read its documentation. And it is not a method.
You need to get away from the String class. Remember String is not intended for changes.
I shall let you do some lateral thinking before telling you anything, but at least three ways to do it came to mind in a minute. They all have similarities.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39079
    
  23
I see Ernest has beaten me to it with the code tags. Thank you.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

I don't want to simply write the code for you, but...

Imagine that you are a tiny bug, and you can only look at one character of the string at a time. You crawl from left to right over the characters, and yell out the ones that aren't spaces.

Now, how could you implement that in code? You need to visit each character. You can read a character using one of String's member functions (look at the javadocs for java.lang.String) and you can use a loop (what kind of loop?) to repeat for each character.


[Jess in Action][AskingGoodQuestions]
Rd Dari
Ranch Hand

Joined: Feb 22, 2010
Posts: 211

Ya Campbell,
You are right that String is immutable in java.
then it is possible to remove extra white space from this string with the other way.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Rd Dari wrote:Ya Campbell,
You are right that String is immutable in java.
then it is possible to remove extra white space from this string with the other way.



Yes, String is immutable, so you can't change that String. But you can do what trim() and replaceAll(), etc. do. You can create a new String based on the original, but leaving out the characters you don't want. Consider that, and the previous suggestion to pretend you're a bug walking along the String, and look closely and carefully at the documentation for the java.lang.String class to find what it provides that might help you.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39079
    
  23
Rd Dari wrote: . . . then it is possible to remove extra white space from this string
Of course it is.
with the other way.
What do you mean other way? You mean other ways, surely.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3016
    
  10
I note that replaceAll() was not mentioned as a method you aren't allowed to use. That's the one that I would use, if it's allowed, to solve this problem. However (a) it's quite possible the interviewer doesn't want you to use that, either; (b) you need to know a bit about regular expressions to use replaceAll() here; and (c) it's definitely good practice to do this without replaceAll(), using the hints the others have been giving. It's probably useful to figure out how to do it several different ways, as Campbell noted.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Remove extra space from a String without split(),trim() and stringTokenizer() method?