在java中按字符串拆分字符串

时间:2023-02-04 16:23:34

I'm getting a string from the web looking like this:

我从网上得到一个字符串,如下所示:

Latest Episode@04x22^Killing Your Number^May/15/2009

Then I need to store 04x22, Killing Your Number and May/15/2009 in diffent variables, but it won't work.

然后我需要在不同的变量中存储04x22,Killing Your Number和May / 15/2009,但它不起作用。

String[] all = inputLine.split("@");
String[] need = all[1].split("^");
show.setNextNr(need[0]);
show.setNextTitle(need[1]);
show.setNextDate(need[2]);

Now it only stores NextNr, with the whole string

现在它只存储NextNr,包含整个字符串

04x22^Killing Your Number^May/15/2009

What is wrong?

哪里不对?

5 个解决方案

#1


32  

String.split(String regex)

String.split(String regex)

The argument is a regualr expression, and ^ has a special meaning there; "anchor to beginning"

参数是一个regualr表达式,^具有特殊含义; “锚定到开始”

You need to do:

你需要这样做:

String[] need = all[1].split("\\^");

String [] need = all [1] .split(“\\ ^”);

By escaping the ^ you're saying "I mean the character '^' "

通过逃避^你说“我的意思是字符'^'”

#2


23  

If you have a separator but you don't know if it contains special characters you can use the following approach

如果您有分隔符但不知道它是否包含特殊字符,则可以使用以下方法

String[] parts = Pattern.compile(separator, Pattern.LITERAL).split(text);

#3


7  

Using guava, you can do it elegantly AND fast:

使用番石榴,你可以优雅而快速地做到:

private static final Splitter RECORD_SPLITTER = Splitter.on(CharMatcher.anyOf("@^")).trimResults().omitEmptyStrings();

...

Iterator<String> splitLine = Iterables.skip(RECORD_SPLITTER.split(inputLine), 1).iterator();

show.setNextNr(splitLine.next());
show.setNextTitle(splitLine.next());
show.setNextDate(splitLine.next());

#4


1  

public static String[] split(String string, char separator) {
    int count = 1;
    for (int index = 0; index < string.length(); index++)
        if (string.charAt(index) == separator)
            count++;
    String parts[] = new String[count];
    int partIndex = 0;
    int startIndex = 0;
    for (int index = 0; index < string.length(); index++)
        if (string.charAt(index) == separator) {
            parts[partIndex++] = string.substring(startIndex, index);
            startIndex = index + 1;
        }
    parts[partIndex++] = string.substring(startIndex);
    return parts;
}

#5


1  

String input = "Latest Episode@04x22^Killing Your Number^May/15/2009";

//split will work for both @ and ^
String splitArr[] = input.split("[@\\^]");

/*The output will be,
 [Latest Episode, 04x22, Killing Your Number, May/15/2009]
*/
System.out.println(Arrays.asList(splitArr));

#1


32  

String.split(String regex)

String.split(String regex)

The argument is a regualr expression, and ^ has a special meaning there; "anchor to beginning"

参数是一个regualr表达式,^具有特殊含义; “锚定到开始”

You need to do:

你需要这样做:

String[] need = all[1].split("\\^");

String [] need = all [1] .split(“\\ ^”);

By escaping the ^ you're saying "I mean the character '^' "

通过逃避^你说“我的意思是字符'^'”

#2


23  

If you have a separator but you don't know if it contains special characters you can use the following approach

如果您有分隔符但不知道它是否包含特殊字符,则可以使用以下方法

String[] parts = Pattern.compile(separator, Pattern.LITERAL).split(text);

#3


7  

Using guava, you can do it elegantly AND fast:

使用番石榴,你可以优雅而快速地做到:

private static final Splitter RECORD_SPLITTER = Splitter.on(CharMatcher.anyOf("@^")).trimResults().omitEmptyStrings();

...

Iterator<String> splitLine = Iterables.skip(RECORD_SPLITTER.split(inputLine), 1).iterator();

show.setNextNr(splitLine.next());
show.setNextTitle(splitLine.next());
show.setNextDate(splitLine.next());

#4


1  

public static String[] split(String string, char separator) {
    int count = 1;
    for (int index = 0; index < string.length(); index++)
        if (string.charAt(index) == separator)
            count++;
    String parts[] = new String[count];
    int partIndex = 0;
    int startIndex = 0;
    for (int index = 0; index < string.length(); index++)
        if (string.charAt(index) == separator) {
            parts[partIndex++] = string.substring(startIndex, index);
            startIndex = index + 1;
        }
    parts[partIndex++] = string.substring(startIndex);
    return parts;
}

#5


1  

String input = "Latest Episode@04x22^Killing Your Number^May/15/2009";

//split will work for both @ and ^
String splitArr[] = input.split("[@\\^]");

/*The output will be,
 [Latest Episode, 04x22, Killing Your Number, May/15/2009]
*/
System.out.println(Arrays.asList(splitArr));