【Codewars】Abbreviate a Two Word Name

Codewars里的 8kyu Kata。

题目说明:

Write a function to convert a name into initials. This kata strictly takes two words with one space in between them.

The output should be two capital letters with a dot seperating them.

It should look like this:

Sam Harris => S.H

Patrick Feeney => P.F

把两个单词搞成缩写的形式。形如 Sam Harris -> S.H。

很简单的题目。

解题代码:

public class AbbreviateTwoWords {
    public static String abbrevName(String name) {
        //name = name.toUpperCase();
        //int index = name.indexOf(' ');
        //String ch = name.substring(index+1, index+2);
        //name = name.substring(0, 1).concat(".");
        //name = name.concat(ch);
        name = name.substring(0, 1).concat(".").concat(name.substring(name.indexOf(' ')+1, name.indexOf(' ')+2)).toUpperCase();
        return name;
    }
}

个人总结:

从简单开始刷起题目吧,这道题可以试试正则表达式。

你可能感兴趣的:(Codewars,数据结构与算法,数组,字符串,Java,数组,字符串,Java,Codewars)