dojoBeginner
dojoBeginner

Reputation: 449

trimming the string

I have java String of say length 10 .Now i want to reduce it to lenthg of 5 .Is there something i can do like we do in C as shown below

 str[6] = '\0' ; //insert null at 6th place so remaining string is ignored.

I dont want to use inbuilt API of java to do this.The main problem that i wanted to solve is i wanted to remove duplicate characters in string .Now after removing duplicate characters string size is reduced so i want to remove remaining 5 characters.

Upvotes: 1

Views: 201

Answers (10)

Sajad
Sajad

Reputation: 2363

As Strings in java is immutable, The Best solution is using of

String.subString()

method!

Upvotes: 0

Stephen C
Stephen C

Reputation: 719729

I wonder why this is tagged as an 'interview-question'?

Anyway, if I was asked this in an interview I'd answer:

  1. Strings are immutable.

  2. The solution is to use String.subString(...).

    Pause to see if the interviewer is expecting more ...

  3. Actually ... if you are really desperate, you can change a String ... using reflection.

  4. But you really, really don't want to do that because the String's char array could be shared with other Strings (or the String could be interned) and changing your string could be changing others at the same time.

  5. Besides, the JLS says that changing a final by backdoor means has indeterminate effects; e.g. vis-a-vis the memory model.

Upvotes: 2

rurouni
rurouni

Reputation: 2445

If you want to remove duplicate characters it might be better to create

StringBuilder sb = new StringBuilder(str);

and use e.g.

sb.delete(start, end);

to remove characters.

Upvotes: 0

Alan Escreet
Alan Escreet

Reputation: 3559

Why don't you want to use the Java API? With String and use of the classes in the regex package, you can do exactly what you want (remove duplicate characters) without messing around with character arrays.

Upvotes: 0

Danilo Tommasina
Danilo Tommasina

Reputation: 1760

// This would be the more or less equivalent in java

str = str.substring( 0, 7 );

Upvotes: 1

Moystard
Moystard

Reputation: 1837

You can instantiate a new String object giving your array of characters and specifying the length of this new String. Indeed String are immutable, an instantiation of a new one is done automatically when modifying an existing String.

String API Reference

Upvotes: 0

G-Man
G-Man

Reputation: 1331

You can use substring to cut off the part you need.

   String name="Hello World";

   //This will print the substring starting from index 6        
    System.out.println(name.substring(6));

    /*
    This will print the substring starting from index 0 up to 4 not 5.
    IMPORTANT: Here startIndex is inclusive while endIndex is exclusive.
    */

    System.out.println(name.substring(0,5));
 /*
 OUTPUT of the above given Java substring Example would be:
 World
 Hello
 */

Upvotes: 0

Peter Knego
Peter Knego

Reputation: 80350

Strings are immutable, so when you removed duplicate characters you already got a new string.

Upvotes: 1

Uros K
Uros K

Reputation: 3384

Take a look at StringBuffer class http://download.oracle.com/javase/1.5.0/docs/api/java/lang/StringBuffer.html

Upvotes: 2

blubb
blubb

Reputation: 9910

Strings in Java are immutable, as such you cannot modify the string but have to create a new one. As you cannot get your fingers on the underlying char[] of the String either, the only way to achieve your goal is using the API methods:

String s = "blah blah blah";
s = s.substring(0, 5);

Upvotes: 4

Related Questions