「Java」Stringクラスの offsetByCodePoints()で文字を取り出す

2022年6月26日

関数
public int java.lang.String.offsetByCodePoints(int index, int codePointOffset)
パラメータ:
index – オフセットされるインデックス
codePointOffset – オフセット(コード・ポイント数)
戻り値:
このString内でのインデックス
Javaコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.example;
public class CharArrayDemo {
public static void main(String[] args) {
String target = "関東や東海で危険な暑さ";
for (int i = 0; i < target.length(); i = target.offsetByCodePoints(i, 1)) {
System.out.println(Character.toChars(target.codePointAt(i)));
}
}
}
package com.example; public class CharArrayDemo { public static void main(String[] args) { String target = "関東や東海で危険な暑さ"; for (int i = 0; i < target.length(); i = target.offsetByCodePoints(i, 1)) { System.out.println(Character.toChars(target.codePointAt(i))); } } }
package com.example;

public class CharArrayDemo {

      public static void main(String[] args) {
            String target = "関東や東海で危険な暑さ";

            for (int i = 0; i < target.length(); i = target.offsetByCodePoints(i, 1)) {
                  System.out.println(Character.toChars(target.codePointAt(i)));
            }

      }

}

結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
関 東 や 東 海 で 危 険 な 暑 さ
関
東
や
東
海
で
危
険
な
暑
さ

 

Java

Posted by arkgame