Suppose I have a String in java with the value "hELLo".
How do I convert it into "heLLo" (The second character must be lowercase) ?
Suppose I have a String in java with the value "hELLo".
How do I convert it into "heLLo" (The second character must be lowercase) ?
There are lot more other ways, but best would be to have a good read at few articles or this.
As simple as that, use java.lang.String#replace
public static void main(String[] args) {
String original = "hELLo";
String modified = original.replaceFirst("E" , "e");
System.out.println(modified);
}
output
heLLo
You can also use replaceAll, for replacing all occurrences of letter.
Edit - Replace 2nd char always
public static void main(String[] args) {
String original = "hELLo";
char secondChar = original.charAt(1);
String modified = original.replaceFirst(String.valueOf(secondChar) , String.valueOf(secondChar).toLowerCase());
System.out.println(modified);
}
output
heLLo
Using String substring
public static void main(String[] args) {
String original = "hELLo";
String secondChar = original.substring(1,2);
String modified = original.replaceFirst(secondChar , secondChar.toLowerCase());
System.out.println(modified);
}
String str = "hELLo";
String str1 = str.replace("E","e");
use replace() of String class