You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将含Tab与换行的字符串拆分为两个String数组?

编辑于2022.10.02 - 问题已解决,可查看文末内容。

文本拆分需求

需要编写程序将一段文本字符串拆分为两个String[]数组,输入字符串text格式如下:

voc1    deut1
voc2    deut2
voc3    deut3
voc4    deut4

两个目标数组需按以下方式填充:

splits[0]="voc1"
splits[1]="voc2"
splits[2]="voc3"
splits[3]="voc4"

lines[0]="deut1"
lines[1]="deut2"
lines[2]="deut3"
lines[3]="deut4"

问题现象

运行旧代码时,数组第一个元素[0]能正常填充,但从[1]开始出现混乱,示例如下:

splits[1]: deut2
voc3
lines[1]: voc2  deut2

旧版本问题代码

public class raplacer {

    public static void main(String[] args) {
        String text= "voc1  deut1\nvoc2 deut2\nvoc3 deut3\nvoc4 deut4";

        String[] splits = null;
        String[] lines = null;
        int numbSplits =0;
        int numbLines =0;

        for(int i=0; i<3;i++) {
            System.out.print("nextLine:  " + text.indexOf("\n"));
            System.out.println("    nextTab:   " + text.indexOf("   "));
            if(text.indexOf("\n")>text.indexOf("    ")) {
                splits=text.split(" ");
                splits[numbSplits+1]="";
                text=text.replaceFirst(splits[numbSplits], "");
                System.out.println("Text:\n" + text);
                text=text.replaceFirst("    ", "");
                System.out.println("Text:\n" + text);
                numbSplits++;
                System.out.println("Splitet a Tab away");
            }
            else if(text.indexOf("\n")<text.indexOf("   ")) {
                lines=text.split("\n");
                lines[numbLines+1]="";
                text=text.replaceFirst(lines[numbLines], "");
                System.out.println("Text:\n" + text);
                text=text.replaceFirst("\n", "");
                System.out.println("Text:\n" + text);
                numbLines++;
                System.out.println("Splited a Line away");
            }
        }
        System.out.println("Text:\n" + text);
        System.out.println();
        System.out.println("splits0: " + splits[0]);
        System.out.println("splits1: " + splits[1]);
        System.out.println();
        System.out.println("lines0: " + lines[0]);
        System.out.println("lines1: " + lines[1]);
    }
}

编辑于2022.10.02 - 经过修改已解决问题,以下是实现版本:

待拆分的完整字符串传入变量text:

text=text+"\n   ";
String[] splits = null;
String[] lines = null;
int numbSplits =0;
int numbLines =0;
int run = 0;
while(text.length()>4) {

    //DEBUG
    run++;
    try{
        Thread.sleep(100);
    }
    catch (InterruptedException ex){
    }
    System.out.println("LENGHT: "+text.length());
    System.out.println("NEXT TAB " + text.indexOf(" ") + " NEXT LINE " + text.indexOf("\n"));
    if(run == 5||run == 10||run == 15||run == 20) {
        System.out.println(text);
        try{
            Thread.sleep(1300);
        }
        catch (InterruptedException ex){}
    }
    if(run==20) {
        break;
    }

    //DEBUG
    if(text.indexOf("\n")>text.indexOf("    ")) {
        splits=text.split(" ",0);
        spanischList.add(splits[0]);
        text=text.replaceFirst(spanischList.get(numbSplits), "");
        text=text.replaceFirst("    ", "");
        splits=null;
        numbSplits++;
    }
    else if(text.indexOf("\n")<text.indexOf("   ")) {
        lines=text.split("\n");
        deutschList.add(lines[0]);
        text=text.replaceFirst(deutschList.get(numbLines), "");
        text=text.replaceFirst("\n", "");
        lines=null;
        numbLines++;
        nrOfVoc +=1;
    }
}

(DEBUG代码块可注释掉,仅用于调试)

另外补充:目前电脑无法处理öäüñ字符,因无法修改编码为UTF-8,这是另一个独立问题。


内容的提问来源于stack exchange,提问作者Martin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 23:10:29