JAVAの照会文字列の分割方法

次のように分割を使用することで今日は、突然の状況を考えた文字列を分割しました:

String str="aaaaaaaab";
String arr[]=str.split("aa");

配列arrの長さはどのくらいある、頼みますか?
strが「baaaaaaaa」であればそれ

	String str="baaaaaaaa";

それはSTR = "aaaaaaaab" の場合

String str="aaaaaaaab";

それはSTR = "baaaaaaaab" の場合

String str="baaaaaaaab";

まあ、我々はプログラムでそれを確認する必要があります。

public class Test {

	public static void main(String[] args) {
		String str="aaaaaaaa";
		String [] arr=str.split("aa");
		System.out.println("字符串aaaaaaaa分割的数组长度为:"+arr.length);
		
		str="baaaaaaaa";
		arr=str.split("aa");
		System.out.println("字符串baaaaaaaa分割的数组长度为:"+arr.length);
		
		str="aaaaaaaab";
		arr=str.split("aa");
		System.out.println("字符串aaaaaaaab分割的数组长度为:"+arr.length);
		
		str="baaaaaaaab";
		arr=str.split("aa");
		System.out.println("字符串baaaaaaaab分割的数组长度为:"+arr.length);

	}
}

上記のコードを実行して出力され
ここに画像を挿入説明
、もしあれば、それがダウンして見ていきます、あなたは少し驚いていない結果を確認します。
今度は、分割の方法により、見た図ソースは(正規表現、0)メソッド分割を呼び出し、0を渡します。

  public String[] split(String regex) {
        return split(regex, 0);
    }

ソースコードを表示し続けます

 public String[] split(String regex, int limit) {
        /* fastpath if the regex is a
         (1)one-char String and this character is not one of the
            RegEx's meta characters ".$|()[{^?*+\\", or
         (2)two-char String and the first char is the backslash and
            the second is not the ascii digit or ascii letter.
         */
        char ch = 0;
        if (((regex.value.length == 1 &&
             ".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||
             (regex.length() == 2 &&
              regex.charAt(0) == '\\' &&
              (((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&
              ((ch-'a')|('z'-ch)) < 0 &&
              ((ch-'A')|('Z'-ch)) < 0)) &&
            (ch < Character.MIN_HIGH_SURROGATE ||
             ch > Character.MAX_LOW_SURROGATE))
        {
            int off = 0;
            int next = 0;
            boolean limited = limit > 0;
            ArrayList<String> list = new ArrayList<>();
            while ((next = indexOf(ch, off)) != -1) {
                if (!limited || list.size() < limit - 1) {
                    list.add(substring(off, next));
                    off = next + 1;
                } else {    // last one
                    //assert (list.size() == limit - 1);
                    list.add(substring(off, value.length));
                    off = value.length;
                    break;
                }
            }
            // If no match was found, return this
            if (off == 0)
                return new String[]{this};

            // Add remaining segment
            if (!limited || list.size() < limit)
                list.add(substring(off, value.length));

            // Construct result
            int resultSize = list.size();
            if (limit == 0) {
                while (resultSize > 0 && list.get(resultSize - 1).length() == 0) {
                    resultSize--;
                }
            }
            String[] result = new String[resultSize];
            return list.subList(0, resultSize).toArray(result);
        }
        return Pattern.compile(regex).split(this, limit);
    }

関係は、最終的にダウンPattern.compile(正規表現).split(この限界)コードのこの部分、基礎Paのコードを実行するショーがあります。

  public String[] split(CharSequence input, int limit) {
        int index = 0;
        boolean matchLimited = limit > 0;
        ArrayList<String> matchList = new ArrayList<>();
        Matcher m = matcher(input);

        // Add segments before each match found
        while(m.find()) {
            if (!matchLimited || matchList.size() < limit - 1) {
                if (index == 0 && index == m.start() && m.start() == m.end()) {
                    // no empty leading substring included for zero-width match
                    // at the beginning of the input char sequence.
                    continue;
                }
                String match = input.subSequence(index, m.start()).toString();
                matchList.add(match);
                index = m.end();
            } else if (matchList.size() == limit - 1) { // last one
                String match = input.subSequence(index,
                                                 input.length()).toString();
                matchList.add(match);
                index = m.end();
            }
        }

        // If no match was found, return this
        if (index == 0)
            return new String[] {input.toString()};

        // Add remaining segment
        if (!matchLimited || matchList.size() < limit)
            matchList.add(input.subSequence(index, input.length()).toString());

        // Construct result
        int resultSize = matchList.size();
        if (limit == 0)
            while (resultSize > 0 && matchList.get(resultSize-1).equals(""))
                resultSize--;
        String[] result = new String[resultSize];
        return matchList.subList(0, resultSize).toArray(result);
    }

コードとは、値を持っていますが、値が空である最終一致リストのコレクションを見ることができます

  while (resultSize > 0 && matchList.get(resultSize-1).equals(""))
                resultSize--;

コードのこの作品は、最初の裁判官最後のものは値がない場合、それはカットされ、というように、我々は結果が上記の手順で表示されているので、驚くべきことではないされていない、空ではありません。
だから私たちは大胆に最後の数が空の場合は、分割の方法は、文字列を分割使用して、まとめることができ、空いている位置が削除されます。
私のマイクロチャネル公共番号ヨーヨーの懸念へようこそ
ここに画像を挿入説明

おすすめ

転載: blog.csdn.net/yu805894501/article/details/94651873