【问题标题】:String manipulation of function names函数名的字符串操作
【发布时间】:2016-04-26 10:36:34
【问题描述】:

对于这个 Kata,我获得了 PEP8 格式的随机函数名称,我要将它们转换为 camelCase。

(输入)get_speed == (输出)getSpeed .... (输入)set_distance == (输出)setDistance

我对用伪代码编写的一种方法有所了解:

loop through the word,
    if the letter is an underscore
        then delete the underscore
        then get the next letter and change to a uppercase
    endIf
endLoop
return the resultant word

但我不确定最好的方法是创建一个 char 数组并循环遍历元素,然后在查找下划线时删除该元素并获取下一个索引并更改为大写,这样会不会更有效。

或者使用递归会更好:

function camelCase takes a string
    if the length of the string is 0,
        then return the string
    endIf
    if the character is a underscore
        then change to nothing,
        then find next character and change to uppercase
        return the string taking away the character
    endIf
    finally return the function taking the first character away

请有任何想法,寻找处理此问题的有效方法。谢谢:)

【问题讨论】:

    标签: java string loops recursion


    【解决方案1】:

    我会选择这个:

    divide given String by underscore to array
    from second word until end take first letter and convert it to uppercase
    join to one word
    

    这将在 O(n) 中工作(遍历所有名称 3 次)。对于第一种情况,使用这个函数:

    str.split("_");
    

    对于大写使用这个:

    String newName = substring(0, 1).toUpperCase() +  stre.substring(1);
    

    但请确保您先检查字符串的大小...

    编辑 - 添加实现

    看起来像这样:

    public String camelCase(String str) {
            if (str == null ||str.trim().length() == 0) return str;
            String[] split = str.split("_");
            String newStr = split[0];
            for (int i = 1; i < split.length; i++) {
                newStr += split[i].substring(0, 1).toUpperCase() +  split[i].substring(1);
            }
            return newStr;
        }
    

    对于输入:

    "test"
    "test_me"
    "test_me_twice"
    

    它返回:

    "test"
    "testMe"
    "testMeTwice"
    

    【讨论】:

    • 如果我有一个这样的函数名:set_the_speed 会这样吗?因为看这个,它只适用于有一个下划线的字符串
    • 第二步是通过拆分数组,因此您从第二个(位置 1)开始查找每个单词。因此,如果您有“set_the_speed”,您会得到:首先将其拆分为:{set, the, speed},然后从位置 1 到数组的末尾,为第一个单词设置大写,所以您得到这个数组:{set , The, Speed},然后将它们加入“setTheSpeed”
    【解决方案2】:

    迭代字符串而不是递归会更简单。

    String pep8 = "do_it_again";
    StringBuilder camelCase = new StringBuilder();
    
    for(int i = 0, l = pep8.length(); i < l; ++i) {
        if(pep8.charAt(i) == '_' && (i + 1) < l) {
            camelCase.append(Character.toUpperCase(pep8.charAt(++i)));
        } else {
            camelCase.append(pep8.charAt(i));
        }
    }
    
    System.out.println(camelCase.toString()); // prints doItAgain
    

    【讨论】:

      【解决方案3】:

      您提出的问题是使用迭代方法还是递归方法。对于这种情况,我会选择递归方法,因为它简单易懂,不需要太多资源(只有一个数组,没有新的堆栈帧等),尽管这对于这个例子并不重要。

      递归对于分而治之的问题很有用,但我认为它并不适合这种情况,尽管它是可能的。

      您描述的算法的迭代实现可能如下所示:

      StringBuilder buf = new StringBuilder(input);
      for(int i = 0; i < buf.length(); i++){
          if(buf.charAt(i) == '_'){
              buf.deleteCharAt(i);
              if(i != buf.length()){ //check fo EOL
                  buf.setCharAt(i, Character.toUpperCase(buf.charAt(i)));
              }
          }
      }
      return buf.toString();
      

      对 EOL 的检查不是给定算法的一部分,如果输入字符串从不以 '_' 结尾,则可以省略

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-05-11
        • 2021-09-06
        • 2015-01-19
        • 2020-09-27
        • 2012-10-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多