/**
* @author lautturi.com
* Java example: remove chars from string in java
*/
import java.util.*;
public class Lautturi {
public static void main(String[] args) {
String str = "hello lautturi java world!";
// remove all l letters
String str1 = str.replace("l", "");
System.out.println(str1);
// remove the last letter
String str2 = str.substring(0,str.length()-1);
System.out.println(str2);
// remove the letter from string at the position idx
int idx = 5;
String str3 = "";
if(idx>0 && idx<str.length()) {
str3 = str.substring(0,idx-1)+str.substring(idx,str.length()-1);
}
else {
str3 = str;
}
System.out.println(str3);
}
}
output:
heo autturi java word! hello lautturi java world hell lautturi java world