-7

Suppose I have a String in java with the value "hELLo".

How do I convert it into "heLLo" (The second character must be lowercase) ?

Sweeper
  • 213,210
  • 22
  • 193
  • 313
Lorren112
  • 93
  • 1
  • 2
  • 6

3 Answers3

2

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);
    }
Community
  • 1
  • 1
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
0
    String str = "hELLo";
    String str1 = str.replace("E","e");

use replace() of String class

K.S.R
  • 1
  • 3
-2

String str = "hELLo";
String str1 = str.replace("E","e");

use replace() of String class

  • 1
    It is a comment, please explain it further more otherwise your answer will be closed under **not an answer** tag. – surajs1n May 06 '16 at 04:38