Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 解析管道分隔字符串_Java_String_Parsing_Pipe - Fatal编程技术网

Java 解析管道分隔字符串

Java 解析管道分隔字符串,java,string,parsing,pipe,Java,String,Parsing,Pipe,我使用以下代码来解析字符串:package org.datacontract public class TestParsing { public static void main(String[] args){ String test = "6000FCI|6|22|122548"; String[] result = test.split("\\|"); for(String s : result){ Sy

我使用以下代码来解析字符串:package org.datacontract

public class TestParsing {


    public static void main(String[] args){
        String test = "6000FCI|6|22|122548";

        String[] result = test.split("\\|");

        for(String s : result){
            System.out.println(s);
        }
    }
}
我的输出是:

如何仅在输出为的情况下获取第一个值:
6000FCI

如果您只需要第一个值,并且您的输入非常简单,则不需要正则表达式,而是执行更简单的操作:

test.substring(0, test.indexOf('|'));
这可以更有效地处理。否则,您将只访问结果数组第一个索引处的值

注意:如果
test
可能不包含管道,请执行以下操作:

int index = test.indexOf('|');
String result = index == -1 ? test : test.substring(0, index);

String result=test.split(“\\\\”)[0]?是否只获取数组中的第一个值<代码>字符串myString=result[0]只有在原始字符串中至少有一个
|
时才起作用。是的,但由于输入是硬编码的,我假设OP想知道如何从给定输入中提取此信息。
int index = test.indexOf('|');
String result = index == -1 ? test : test.substring(0, index);