1

私はJavaとエンコーディングが初めてなので、この質問です。

私は基本的に、16 進数値で表される有効な文字のセットを含むテキスト ファイルを持っています。例: 0x2000-0x4002,0x5002-0x5F00

これで、文字列を含む別のファイルができました。例: このファイルを使用しようとしています。

私の問題は、2番目のファイルの各文字が有効で、上記のファイルで説明されている範囲内にあるかどうかを確認することです。

だからこれは私がやっていることです:

public class Test
{
   //This is a function used to build the ranges.
   public void build range() {}

   //This function will test whether the string str is in given range.
   public bool check range(String str)
   {
      int codePointCount = str.codePointCount(0, str.length());
      for( in ti =0; i< codePointCount; i++)
      {
          int value = str.codePointAt(i);
          if( value >= 2000 && value <= 4002 )
             continue;
          if( value >= 5002 && value <= 5F00 )
             continue;
          return false;
      }
      return true;
   } 
}

このコードが正しいかどうか、またはエンコードに関して何か不足しているかどうかをお知らせください。

4

2 に答える 2

2

正規表現を使用することをお勧めします。これがアイデアです

    boolean ok = !str.matches(".*[^\u2000-\u4002\u5002-\u5F00].*");
于 2013-02-11T11:14:53.443 に答える
0

最初に小さな修正:

  for (int i = 0; i < str.length(); )
  {
      int value = str.codePointAt(i);
      i += Character.charCount(value);
      if( value >= 0x2000 && value <= 0x4002 )
         continue;
      if( value >= 0x5002 && value <= 0x5F00 )
         continue;
      return false;
  }

しかし、長さ/読みやすさの点で、@EvgeniyDororfeevの答えが最適です。

于 2013-02-11T11:22:04.437 に答える