2

i have following piece of code

import java.io.*;
import java.util.*;
class MainDriver{
   public static void main(String aa[]){
        Scanner reader = new Scanner (System.in);
        String name;
        System.out.println("enter your name");
        name = reader.nextLine();
        name.trim();
        System.out.println (name);
    }
}

And i am wondering about output if i give whitespaces in input

input

 bayant singh

output

 bayant singh

So why name.trim; is not working here?As i am guessing output to be

bayantsingh //no spaces
OldSchool
  • 2,123
  • 4
  • 23
  • 45

3 Answers3

8

trim removes whitespaces at either end of the string. to remove all spaces try:

name = name.replace(" ","");

for all whitespace of any kind it has been pointed out to me you can use the regex:

name = name.replaceAll("\\s","");
Adam Yost
  • 3,616
  • 23
  • 36
3

Your first problem is that Strings are immutable, which means its methods can't edit its state (stored characters) but instead they create new updated String instance. So

name.trim();

creates and returns new trimmed String which you are ignoring because you are not storing result of this method anywhere. If you want name reference to hold trimmed String you should use

name = name.trim();

Your second problem is that if you want to change " bayant singh" to "bayantsingh" you shouldn't use trim method because removes only spaces only at start and end of your String.
If you want to remove all spaces you should use

name = name.replace(" ","");
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • this is correct for trim, not correct for what he wants. trim does not remove spaces in between words, only at the beginning and ending – Adam Yost Jun 06 '14 at 18:28
  • @AdamYost Yes, I was in the middle of editing my answer. I am not sure why I didn't described it sooner (maybe OP added that part later but before 5 min grace time so this change is not in edit history). – Pshemo Jun 06 '14 at 18:29
  • @pshemo String's methods don't change its state because of pass by value nature.is it so? – OldSchool Jun 06 '14 at 18:44
  • @Bayant_singh This things are not related since all methods are pass-by-value (there is no pass-by-reference in Java). If you pass some value to method you can change state of instance on which you invoked this method. That is how setters work. [Strings are meant to be immutable](http://stackoverflow.com/q/9544182/1393766). If you need more informations about immutability you can find many good questions explaining it like http://stackoverflow.com/questions/279507/what-is-meant-by-immutable or http://stackoverflow.com/questions/214714/mutable-vs-immutable-objects – Pshemo Jun 06 '14 at 18:56
1

If you want replace all spaces use .replaceAll()

name = name.replace(" ",""); // replace first space
name = name.replaceAll(" ",""); // replace all spaces in the string
Lugaru
  • 1,430
  • 3
  • 25
  • 38
  • 1
    `name.replace(" ","")` will replace all spaces, not just first. Difference between `replace` and `replaceAll` is that it doesn't take regex as searched parameter but literal. – Pshemo Jun 06 '14 at 18:33