gpt4 book ai didi

java - 在 Android 上从 USB 文本中分割 str 和连续制表符后留下的空字段难以识别

转载 作者:行者123 更新时间:2023-12-01 05:15:23 24 4
gpt4 key购买 nike

我正在从 USB 驱动器将一个文本文件读入 Android 4.0 平板电脑,其中许多行都以选项卡开头。作为语言测验实用程序,典型的台词是

[tab]an experience[crlf]

在拆分时,我有一个空数组元素,到目前为止,我只能通过测试 stringJustSplit[0].length( ) == 0 来检测该元素。这感觉不安全,因为长度是元素的一个属性,而不是它等于的属性。当然 ==""== null 并没有让我有任何收获。

我看到了 RemoveEmptyEntries 选项,但我宁愿保留空元素,并且程序会有意地响应它们。

是否有一个语句可以处理它们?

日志产生:

    ---Array trial---(6229): Start
---Array trial---(6229): Located by length( )==0.
---Array trial---(6229): End

从此方法:

while ((Fileline = buffered_reader.readLine( )) != null) {
line2array = Fileline.split("\\t");
String[] tester = new String[1];
String dummyStr = "|element1";
String[] emptyField = dummyStr.split("\\|");
if (true) {Log.v(T, "Start results");}
if (line2array[0] == emptyField[0]){Log.v(T, "Located by array element created similar way.");}
if (line2array[0] == tester[0]) {Log.v(T, "Located by comparison w just-made array.");}
if (line2array[0].length( ) == 0) {Log.v(T, "Located by length( ) == 0.");}
if (line2array[0] == "") {Log.v(T, "Located by zero-len string.");}
if (line2array[0] == null) {Log.v(T, "Located by null.");}
if (true) {Log.v(T, "End");}
}

最佳答案

在Java中,需要使用equals方法来比较字符串的内容,==会比较变量是否引用了内存中的相同地址。

所以你的代码是:

// can't call .equals on something that is null
if (line2array[0] != null && line2array[0].equals(emptyField[0])) {
Log.v(T, "Located by array element created similar way.");
}
if (line2array[0] != null && line2array[0].equals(tester[0])) {
Log.v(T, "Located by comparison w just-made array.");
}
if (line2array[0].length() == 0) {
Log.v(T, "Located by length( ) == 0.");
}
// literals are never null
if ("".equals(line2array[0])) {
Log.v(T, "Located by zero-len string.");
}
if (line2array[0] == null) {
Log.v(T, "Located by null.");
}
if (true) {
Log.v(T, "End");
}

关于java - 在 Android 上从 USB 文本中分割 str 和连续制表符后留下的空字段难以识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11295467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com