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.
Stringsin 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 underlyingchar[]of theStringeither, the only way to achieve your goal is using the API methods: