Reputation: 449
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
Reputation: 2363
As Strings in java is immutable, The Best solution is using of
String.subString()
method!
Upvotes: 0
Reputation: 719729
I wonder why this is tagged as an 'interview-question'?
Anyway, if I was asked this in an interview I'd answer:
Strings are immutable.
The solution is to use String.subString(...)
.
Pause to see if the interviewer is expecting more ...
Actually ... if you are really desperate, you can change a String ... using reflection.
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.
Besides, the JLS says that changing a final
by backdoor means has indeterminate effects; e.g. vis-a-vis the memory model.
Upvotes: 2
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
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
Reputation: 1760
// This would be the more or less equivalent in java
str = str.substring( 0, 7 );
Upvotes: 1
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.
Upvotes: 0
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
Reputation: 80350
Strings are immutable, so when you removed duplicate characters you already got a new string.
Upvotes: 1
Reputation: 3384
Take a look at StringBuffer class http://download.oracle.com/javase/1.5.0/docs/api/java/lang/StringBuffer.html
Upvotes: 2
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