📜  在Java中将 Snake Case字符串转换为 Camel Case

📅  最后修改于: 2021-09-04 08:16:58             🧑  作者: Mango

给定Snake Case 中的字符串,任务是编写一个Java程序将给定的字符串从蛇形大小写转换为驼色大小写并打印修改后的字符串。

例子:

方法一:使用遍历

  1. 这个想法是首先将字符串的第一个字母大写。
  2. 然后转换字符串来字符串建设者。
  3. 然后通过字符从第一个索引遍历字符串的字符到最后一个索引,并检查字符强调然后删除字符并利用下划线的下一个字符。
  4. 打印修改后的字符串。

下面是上述方法的实现:

Java
// Java program for the above approach
  
import java.io.*;
  
class GFG {
  
    // Function to convert snake case
    // to camel case
    public static String
    snakeToCamel(String str)
    {
        // Capitalize first letter of string
        str = str.substring(0, 1).toUpperCase()
              + str.substring(1);
  
        // Convert to StringBuilder
        StringBuilder builder
            = new StringBuilder(str);
  
        // Traverse the string character by
        // character and remove underscore
        // and capitalize next letter
        for (int i = 0; i < builder.length(); i++) {
  
            // Check char is underscore
            if (builder.charAt(i) == '_') {
  
                builder.deleteCharAt(i);
                builder.replace(
                    i, i + 1,
                    String.valueOf(
                        Character.toUpperCase(
                            builder.charAt(i))));
            }
        }
  
        // Return in String type
        return builder.toString();
    }
  
    // Driver Code
    public static void
        main(String[] args)
    {
  
        // Given String
        String str = "geeks_for_geeks";
  
        // Function Call
        str = snakeToCamel(str);
  
        // Modified String
        System.out.println(str);
    }
}


Java
// Java program for the above approach
  
class GFG {
  
    // Function to convert the string
    // from snake case to camel case
    public static String
    snakeToCamel(String str)
    {
        // Capitalize first letter of string
        str = str.substring(0, 1).toUpperCase()
              + str.substring(1);
  
        // Run a loop till string
        // string contains underscore
        while (str.contains("_")) {
  
            // Replace the first occurrence
            // of letter that present after
            // the underscore, to capitalize
            // form of next letter of underscore
            str = str
                      .replaceFirst(
                          "_[a-z]",
                          String.valueOf(
                              Character.toUpperCase(
                                  str.charAt(
                                      str.indexOf("_") + 1))));
        }
  
        // Return string
        return str;
    }
  
    // Driver Code
    public static void
    main(String args[])
    {
        // Given string
        String str = "geeks_for_geeks";
  
        // Print the modified string
        System.out.print(snakeToCamel(str));
    }
}


输出:
GeeksForGeeks

方法 2:使用 String.replaceFirst() 方法

  1. 这个想法是使用 String.replaceFirst() 方法将给定的字符串从蛇形大小写转换为驼色大小写。
  2. 首先,将字符串的第一个字母大写。
  3. 运行循环直到字符串包含下划线 (_)。
  4. 将出现在下划线之后的第一个字母替换为下划线下一个字母的大写形式。
  5. 打印修改后的字符串。

下面是上述方法的实现:

Java

// Java program for the above approach
  
class GFG {
  
    // Function to convert the string
    // from snake case to camel case
    public static String
    snakeToCamel(String str)
    {
        // Capitalize first letter of string
        str = str.substring(0, 1).toUpperCase()
              + str.substring(1);
  
        // Run a loop till string
        // string contains underscore
        while (str.contains("_")) {
  
            // Replace the first occurrence
            // of letter that present after
            // the underscore, to capitalize
            // form of next letter of underscore
            str = str
                      .replaceFirst(
                          "_[a-z]",
                          String.valueOf(
                              Character.toUpperCase(
                                  str.charAt(
                                      str.indexOf("_") + 1))));
        }
  
        // Return string
        return str;
    }
  
    // Driver Code
    public static void
    main(String args[])
    {
        // Given string
        String str = "geeks_for_geeks";
  
        // Print the modified string
        System.out.print(snakeToCamel(str));
    }
}
输出:
GeeksForGeeks

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live