【Codewars】Abbreviate a Two Word Name

博客介绍了 Codewars 里的 8kyu Kata 题目,要求将包含一个空格分隔的两个单词转换为缩写形式,如 Sam Harris 转换为 S.H。还给出解题思路,建议从简单题刷起,可尝试用正则表达式解题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
    }
}

个人总结:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值