开发者

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开发者_运维技巧.


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


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


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.


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


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

str = str.substring( 0, 7 );


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
 */


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


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.


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.


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

String.subString()

method!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜