2

I want to write java code to convert left side strings to right ones.

1234_hello -- 1234_Hello
hello Data -- Hello Data
hELLO data -- Hello data
1234hEllo  -- 1234Hello
heLLO1234hEllo -- Hello1234hEllo
$hello     -- $Hello

Could you please help with the solution? Thank you!

  • Does this answer your question? [How to capitalize the first character of each word in a string](https://stackoverflow.com/questions/1892765/how-to-capitalize-the-first-character-of-each-word-in-a-string) – DevWithZachary Jul 07 '21 at 08:14
  • No, not for each word, only first alphabet in the string. eg. for "hello world" output will be "Hello world". Thanks! – Supriya Shelar Jul 07 '21 at 08:18
  • But also removing capitals inside words? What about your second example, data has a capital D and should remain that way? – DevWithZachary Jul 07 '21 at 08:19
  • 3
    Your spec seems inconsistent. Why is `hELLO` turned into `Hello` if you just want to capitalise the first alpha character? The same goes for `1234hEllo -> 1234Hello` – QBrute Jul 07 '21 at 08:19
  • 1
    What is the problem with a loop? – AndiCover Jul 07 '21 at 08:20
  • How about Recursion ? – Scary Wombat Jul 07 '21 at 08:23
  • Actually I want to work on only first word in the string. I want it with first letter capital and other small. eg for HeLLo1234Data => Hello1234Data, helLo Data => Hello Data, – Supriya Shelar Jul 07 '21 at 08:32

2 Answers2

3

Here is a solution:

public static void main(String[] args) {
    try {
        System.out.println(convertString("1234_hello"));
        System.out.println(convertString("hello Data"));
        System.out.println(convertString("hELLO data"));
        System.out.println(convertString("1234hEllo"));
        System.out.println(convertString("heLLO1234hEllo"));
        System.out.println(convertString("$hello"));
        System.out.println(convertString("$1234hEllo_TTHjjZ"));
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

private static String convertString(String string) {

    String result = string;

    final String regex1 = "^([^a-zA-Z]+)([a-zA-Z])([a-zA-Z]*)([^a-zA-Z].*)$";
    final String regex2 = "^([a-zA-Z])([a-zA-Z]*)([^a-zA-Z].*)$";
    final String regex3 = "^([^a-zA-Z]+)([a-zA-Z])([a-zA-Z]*)$";

    final Pattern pattern1 = Pattern.compile(regex1, Pattern.MULTILINE);
    final Pattern pattern2 = Pattern.compile(regex2, Pattern.MULTILINE);
    final Pattern pattern3 = Pattern.compile(regex3, Pattern.MULTILINE);

    Matcher matcher1 = pattern1.matcher(string);
    Matcher matcher2 = pattern2.matcher(string);
    Matcher matcher3 = pattern3.matcher(string);

    if (matcher1.find()) {
        result = matcher1.group(1) + matcher1.group(2).toUpperCase() + matcher1.group(3).toLowerCase() + matcher1.group(4);
    }
    else if (matcher2.find()) {
        result = matcher2.group(1).toUpperCase() + matcher2.group(2).toLowerCase() + matcher2.group(3);
    }
    else if (matcher3.find()) {
        result = matcher3.group(1) + matcher3.group(2).toUpperCase() + matcher3.group(3).toLowerCase();
    }

    return result;
}

The result is as expected:

1234_Hello
Hello Data
Hello data
1234Hello
Hello1234hEllo
$Hello
$1234Hello_TTHjjZ
Stéphane Millien
  • 3,238
  • 22
  • 36
0

I have a solution for you but it is not efficient:

public static String toCamelCase(String input) {
    StringBuilder output = new StringBuilder();
    for(int i = 0; i < input.length(); i++) {
        if(i == 0) {
            output.append(Character.toUpperCase(input.charAt(i)));
            continue;
        }
        if(Character.isLetter(input.charAt(i))) {
            if(Character.isLetter(input.charAt(i-1))) {
                output.append(Character.toLowerCase(input.charAt(i)));
            } else {
                output.append(Character.toUpperCase(input.charAt(i)));
            }
        } else {
            output.append(input.charAt(i));
        }
    }
    return output.toString();
}
Toxicvipa
  • 16
  • 1