0

I know how to access a certain string spot using a for loop and doing string.substring(i), but I do not know to remove or add letters/numbers.

String.substring(i).replace("x", "");

public static String replace(String s) {
    for (int i=0; i<s.length(); i++) {
            if (s.substring(i) == ... ) {
                //more code
            }
    }
}

When I run this code, it still prints the original String.

Nicholas Chen
  • 29
  • 1
  • 10
  • Too many problems with your code. 1) Strings in Java are immutable, which means if you want a substring operation to "stick," you have to reassign it to another String. 2) Strings in Java are not compared using `==`. Use `equals()` method instead. – Tim Biegeleisen Apr 17 '19 at 04:52
  • Java String class does not provide mentioned methods but there can be different workarounds to achieve this (adding/removing). String will always return new instance as it's immutable. – Zain Ul Abideen Apr 17 '19 at 04:56
  • Do you want to replace a certain character with another, no matter where, or how many times, in the string it appears? – Bohemian Apr 17 '19 at 05:21
  • Note that in java [`String` is immutable](https://stackoverflow.com/questions/1552301/immutability-of-strings-in-java); you can't "set" a character, you have to make a new String. – Bohemian Apr 17 '19 at 05:24
  • check the code it will help you ,use a java compiler. public class HelloWorld{ static void remove(String replaceWord,String fullWord){ String result = fullWord.replace(replaceWord,""); System.out.println("Final Result:"+result); } public static void main(String[] args){ String fullWord = "DataSet"; String removeWord = "Set"; remove(removeWord, fullWord); } } – Bathri Nathan Apr 17 '19 at 06:06

3 Answers3

0

String is immutable in java . You can make StringBuilder object from String and do remove and add opertaion on it. StringBuilder gives you a methods deleteCharAt and setCharAt. You can use them and in the end you can again get your new String back by calling method toString from StringBuilder object.

StringBuilder sb = new StringBuilder("yourString");
for(int i=0;i<sb.length();i++){
    if(sb.charAt(i) == '5'){ // want to compare '5' 
      sb.deleteCharAt(i);
    } else {
      sb.setCharAt(i,'6');
    }
}
String newString = sb.toString;
Khalid Shah
  • 3,132
  • 3
  • 20
  • 39
  • What is the purpose of StringBuilder? – Nicholas Chen Apr 17 '19 at 05:06
  • 1
    @NicholasChen `StringBuilder` is Mutable and it performs additional operations and faster than Strings. Have a look at this https://www.journaldev.com/538/string-vs-stringbuffer-vs-stringbuilder – Khalid Shah Apr 17 '19 at 05:08
0

Mentioned operations cannot be directly achieved by Java String but can be achieved by StringBuilder.

Insert example:

public class App {

public static void main(String[] args) {

    StringBuilder stringBuilder = new StringBuilder("Hello ");
    System.out.println("String :- " + stringBuilder);

    String string = "World";
    stringBuilder.insert(6, string);

    System.out.println("After insertion :- " + stringBuilder.toString());
}
}

Delete example:

public class App {

public static void main(String[] args) {
    StringBuilder stringBuilder = new StringBuilder("Hello World");

    System.out.println("Value:- " + stringBuilder);

    stringBuilder.delete(4, 9);
    System.out.println("After deletion :- " + stringBuilder);
}
}
Zain Ul Abideen
  • 1,617
  • 1
  • 11
  • 25
0

In Java Strings are immutable objects until you reassign the reference it won't point to the modified object.

        String str = "Hello work";

        if(str.substring(6).equals("work")) {
            str = str.replace(str.substring(6), "World!");
        }
gprasadr8
  • 789
  • 1
  • 8
  • 12