📌  相关文章
📜  在Java中将驼峰字符串转换为蛇形字符串

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

给定一个驼峰式字符串,任务是编写一个Java程序,将给定的字符串从驼峰式转换为蛇式,并打印修改后的字符串。

例子:

方法 1:朴素的方法

  • 首先,我们用一个空字符串初始化一个变量“result”,并将第一个字符(小写)附加到它上面。
  • 现在从第一个索引到最后一个索引迭代字符串的每个字符,如果字符是大写字母,我们附加 ‘_’ 和字符(小写)以得到结果,否则只附加字符。

下面是上述方法的实现:

Java
class GFG {
  
    // Function to covert camel case
    // string to snake case string
    public static String camelToSnake(String str)
    {
  
        // Empty String
        String result = "";
  
        // Append first character(in lower case)
        // to result string
        char c = str.charAt(0);
        result = result + Character.toLowerCase(c);
  
        // Tarverse the string from
        // ist index to last index
        for (int i = 1; i < str.length(); i++) {
  
            char ch = str.charAt(i);
  
            // Check if the character is upper case
            // then append '_' and such character
            // (in lower case) to result string
            if (Character.isUpperCase(ch)) {
                result = result + '_';
                result
                    = result
                      + Character.toLowerCase(ch);
            }
  
            // If the character is lower case then
            // add such character into result string
            else {
                result = result + ch;
            }
        }
  
        // return the result
        return result;
    }
  
    public static void main(String args[])
    {
        // Given string str
        String str = "GeeksForGeeks";
  
        // Print the modified string
        System.out.print(camelToSnake(str));
    }
}


Java
class GFG {
  
    // Function to covert camel case
    // string to snake case string
    public static String
    camelToSnake(String str)
    {
        // Regular Expression
        String regex = "([a-z])([A-Z]+)";
  
        // Replacement string
        String replacement = "$1_$2";
  
        // Replace the given regex
        // with replacement string
        // and convert it to lower case.
        str = str
                  .replaceAll(
                      regex, replacement)
                  .toLowerCase();
  
        // return string
        return str;
    }
  
    // Driver Code
    public static void main(String args[])
    {
        // Given string str
        String str = "GeeksForGeeks";
  
        // Print the modified string
        System.out.print(camelToSnake(str));
    }
}


输出:
geeks_for_geeks

方法二:使用 String.replaceAll 方法

  • 这个想法是使用 String.replaceAll 方法将给定的字符串从骆驼案例转换为蛇案例
  • String.replaceAll 方法接受两个参数:正则表达式和替换字符串。它用替换字符串替换正则表达式并打印修改后的字符串。

下面是上述方法的实现:

Java

class GFG {
  
    // Function to covert camel case
    // string to snake case string
    public static String
    camelToSnake(String str)
    {
        // Regular Expression
        String regex = "([a-z])([A-Z]+)";
  
        // Replacement string
        String replacement = "$1_$2";
  
        // Replace the given regex
        // with replacement string
        // and convert it to lower case.
        str = str
                  .replaceAll(
                      regex, replacement)
                  .toLowerCase();
  
        // return string
        return str;
    }
  
    // Driver Code
    public static void main(String args[])
    {
        // Given string str
        String str = "GeeksForGeeks";
  
        // Print the modified string
        System.out.print(camelToSnake(str));
    }
}
输出:
geeks_for_geeks

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