简体   繁体   English

将java中的半角字符串转换为全角

[英]Convert a half width string in java to full width

Consider a half width string 'Hello'.考虑一个半宽字符串“Hello”。 What java library should I use to get its full width equivalent which is 'Hello'.我应该使用什么 java 库来获得它的全宽等价物,即“Helloo”。

Any sample code is also appreciated.任何示例代码也值得赞赏。

Half and full width characters differ by 65248 .半角和全角字符相差65248 So all you need to do is simply add that number to each character.所以你需要做的就是简单地把这个数字加到每个字符上。

Example with stream:流示例:

public static String toFullWidth(String halfWidth) {
  return halfWidth.chars()
    .map(c -> c + 65248)
    .collect(
      StringBuilder::new, 
      (builder, c) -> builder.append((char) c), 
      StringBuilder::append
    )
    .toString();
}

Example with loop:循环示例:

public static String toFullWidthWithLoop(String halfWidth) {
  StringBuilder builder = new StringBuilder();
  for (char c : halfWidth.toCharArray()) {
    builder.append((char) (c + 65248));
  }
  return builder.toString();
}

Try this :尝试这个 :

    String s = "Hello" ;
    System.out.println(s.replaceAll(""," ")) ;

you will get H ello你会得到H ello

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM