Daily Codewars #2019-03-01

Question

문제링크 : [8 kyu]Abbreviate a Two Word Name

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

My Solution

import java.util.Arrays;
import java.util.stream.Collectors;

public class AbbreviateTwoWords {

  public static String abbrevName(String name) {
    return name.split(" ")[0].toUpperCase().charAt(0) + "." + name.split(" ")[1].toUpperCase().charAt(0);
  }
}
import java.util.Arrays;
import java.util.stream.Collectors;

public class AbbreviateTwoWords {

  public static String abbrevName(String name) {
       return Arrays.stream(name.split(" "))
              .map(String::toUpperCase)
              .map(n -> n.substring(0, 1))
              .collect(Collectors.joining("."));
  }
}

@dinglemouse Solution

public class AbbreviateTwoWords {

  public static String abbrevName(String name) {
    return name.toUpperCase().replaceAll("(.).*\\\\s(.).*", "$1.$2");
  }

}

와우! 정규표현식은 위대하다. 한줄로 코딩은 끝나는구나. 아직 정규표현식을 제대로 공부를 하지 못해서 코딩하기엔 무리지만 정규표현식이 정말 대단한 것 같다. 나중에 꼭 공부해야겠다.

@Velimir10 Solution

public class AbbreviateTwoWords {

 public static String abbrevName(String name) {

        String[] init = name.split(" ");
        return init[0].substring(0, 1).toUpperCase().concat("."+ init[1].substring(0, 1).toUpperCase());
    }
}

난 그냥 String + “.” String 으로 문자를 붙였는데 concat을 쓸 것은 생각을 못했다. 아무래서 성능상 String 을 따로 써서 String 객체가 각각 생성 되게 하는 것보다. .concat으로 붙이는 것이 더 좋은 방법 같다.

@scottyboutin Solution