Java string.split是一个拆分字符串

时间:2022-06-01 03:11:35

My data is Like this:

我的数据是这样的:

Smith, Bob; Data; More Data
Doe, John; Data; More Data

If you look below you'll see that I'm trying to split the FullName into first and last. The error is:

如果你看下面你会看到我正在尝试将FullName分成第一个和最后一个。错误是:

"The method split(String) in the type String is not applicable for the arguments (char)"

“String类型中的方法split(String)不适用于参数(char)”

String line = scanner.nextLine();
String[] data = line.split(";");
String[] fullName = data[0].split(',');

3 个解决方案

#1


7  

I explained in my comment that the error could be fixed quite easily: ','",".

我在评论中解释说错误很容易修复:','→“,”。

I'll just give some details as to why this is the case. In Java, single characters enclosed in 's are character literals, which are very different from string literals, enclosed in "s. If you're coming from the world of Python, for example, this might take some time to get used to since there ' and " can be used essentially synonymously.

我将简要介绍一下为何会出现这种情况。在Java中,用s括起来的单个字符是字符文字,它们与字符串文字非常不同,用“s”封装。例如,如果你来自Python的世界,这可能需要一些时间来习惯那里'和'可以基本上同义使用。

In any case, the 1-argument split() method of String accepts only a String which is in turn parsed as a regular expression. That's why you need the double quotes.

在任何情况下,String的1参数split()方法只接受一个String,而String又被解析为正则表达式。这就是你需要双引号的原因。

#2


1  

Switch the single quotes to double

将单引号切换为double

String[] fullName = data[0].split(",");

String [] fullName = data [0] .split(“,”);

#3


0  

String.split(String) takes a String as the lone parameter.

String.split(String)将String作为单独的参数。

public String[] split(String regex)

    Splits this string around matches of the given regular expression.

    This method works as if by invoking the two-argument split method with the
    given expression and a limit argument of zero. Trailing empty strings are
    therefore not included in the resulting array.

    The string "boo:and:foo", for example, yields the following results with
    these expressions:

        Regex   Result
        :   { "boo", "and", "foo" }
        o   { "b", "", ":and:f" }

    Parameters:
        regex - the delimiting regular expression 
    Returns:
        the array of strings computed by splitting this string around matches
        of the given regular expression 
    Throws:
        PatternSyntaxException - if the regular expression's syntax is invalid
    Since:
        1.4
    See Also:
        Pattern

#1


7  

I explained in my comment that the error could be fixed quite easily: ','",".

我在评论中解释说错误很容易修复:','→“,”。

I'll just give some details as to why this is the case. In Java, single characters enclosed in 's are character literals, which are very different from string literals, enclosed in "s. If you're coming from the world of Python, for example, this might take some time to get used to since there ' and " can be used essentially synonymously.

我将简要介绍一下为何会出现这种情况。在Java中,用s括起来的单个字符是字符文字,它们与字符串文字非常不同,用“s”封装。例如,如果你来自Python的世界,这可能需要一些时间来习惯那里'和'可以基本上同义使用。

In any case, the 1-argument split() method of String accepts only a String which is in turn parsed as a regular expression. That's why you need the double quotes.

在任何情况下,String的1参数split()方法只接受一个String,而String又被解析为正则表达式。这就是你需要双引号的原因。

#2


1  

Switch the single quotes to double

将单引号切换为double

String[] fullName = data[0].split(",");

String [] fullName = data [0] .split(“,”);

#3


0  

String.split(String) takes a String as the lone parameter.

String.split(String)将String作为单独的参数。

public String[] split(String regex)

    Splits this string around matches of the given regular expression.

    This method works as if by invoking the two-argument split method with the
    given expression and a limit argument of zero. Trailing empty strings are
    therefore not included in the resulting array.

    The string "boo:and:foo", for example, yields the following results with
    these expressions:

        Regex   Result
        :   { "boo", "and", "foo" }
        o   { "b", "", ":and:f" }

    Parameters:
        regex - the delimiting regular expression 
    Returns:
        the array of strings computed by splitting this string around matches
        of the given regular expression 
    Throws:
        PatternSyntaxException - if the regular expression's syntax is invalid
    Since:
        1.4
    See Also:
        Pattern