[JAVA] Remove/Replace Character from String

1. String.replace(char oldChar, char newChar)

public static void main(String[] args) {
String string1 = "hello world";
String string2 = string1.replace("world", "lady");
System.out.println(string2);
}

//hello lady

2. String.substring(startindex, endindex)
//Remove
String result = string1.substring(0, 5) + string1.substring(6);
System.out.println(result);

//result: helloworld

//replace
String result = string1.substring(0, 5) +"-"+ string1.substring(6);
System.out.println(result);

//result: hello-world

Leave a Reply

Your email address will not be published. Required fields are marked *