如何在Java中拆分字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3481828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:10:52  来源:igfitidea点击:

How to split a string in Java

javastringsplit

提问by riyana

I have a string, "004-034556", that I want to split into two strings:

我有一个字符串 , "004-034556",我想将其拆分为两个字符串:

string1="004";
string2="034556";

That means the first string will contain the characters before '-', and the second string will contain the characters after '-'. I also want to check if the string has '-'in it. If not, I will throw an exception. How can I do this?

这意味着第一个字符串将包含之前的字符'-',第二个字符串将包含之后的字符'-'。我还想检查字符串'-'中是否有。如果没有,我将抛出异常。我怎样才能做到这一点?

采纳答案by BalusC

Just use the appropriate method: String#split().

只需使用适当的方法:String#split().

String string = "004-034556";
String[] parts = string.split("-");
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556

Note that this takes a regular expression, so remember to escape special charactersif necessary.

请注意,这需要一个正则表达式,因此请记住在必要时转义特殊字符

thereare 12 characters with special meanings: the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening parenthesis (, the closing parenthesis ), and the opening square bracket [, the opening curly brace {, These special characters are often called "metacharacters".

12 个具有特殊含义的字符:反斜杠\、插入符号^、美元符号$、句点或点.、竖线或管道符号|、问号?、星号或星号*、加号+、左括号(、右括号),以及[左方括号{、左花括号,这些特殊字符通常被称为“元字符”。

So, if you want to split on e.g. period/dot .which means "any character" in regex, use either backslash \to escape the individual special character like so split("\\."), or use character class []to represent literal character(s) like so split("[.]"), or use Pattern#quote()to escape the entire string like so split(Pattern.quote(".")).

所以,如果你想在如期间分/点.的意思是“任何字符在正则表达式”,请使用反斜杠\逃脱个别特殊字符,像这样split("\\."),或使用字符类[]来表示文字字符(S)像这样split("[.]"),或使用Pattern#quote()以像这样转义整个字符串split(Pattern.quote("."))

String[] parts = string.split(Pattern.quote(".")); // Split on period.

To test beforehand if the string contains certain character(s), just use String#contains().

要预先测试字符串是否包含某些字符,只需使用String#contains().

if (string.contains("-")) {
    // Split it.
} else {
    throw new IllegalArgumentException("String " + string + " does not contain -");
}

Note, this does not take a regular expression. For that, use String#matches()instead.

请注意,这不需要正则表达式。为此,请String#matches()改用。

If you'd like to retain the split character in the resulting parts, then make use of positive lookaround. In case you want to have the split character to end up in left hand side, use positive lookbehind by prefixing ?<=group on the pattern.

如果您想在生成的部分中保留拆分字符,请使用正环视。如果您想让拆分字符在左侧结束,请通过?<=在模式上添加组前缀来使用正向后视。

String string = "004-034556";
String[] parts = string.split("(?<=-)");
String part1 = parts[0]; // 004-
String part2 = parts[1]; // 034556

In case you want to have the split character to end up in right hand side, use positive lookahead by prefixing ?=group on the pattern.

如果您想让拆分字符在右侧结束,请通过?=在模式上添加组前缀来使用正向先行。

String string = "004-034556";
String[] parts = string.split("(?=-)");
String part1 = parts[0]; // 004
String part2 = parts[1]; // -034556

If you'd like to limit the number of resulting parts, then you can supply the desired number as 2nd argument of split()method.

如果您想限制结果部分的数量,那么您可以提供所需的数量作为split()方法的第二个参数。

String string = "004-034556-42";
String[] parts = string.split("-", 2);
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556-42

回答by jjnguy

String[] result = yourString.split("-");
if (result.length != 2) 
     throw new IllegalArgumentException("String not in correct format");

This will split your string into 2 parts. The first element in the array will be the part containing the stuff before the -, and the 2nd element in the array will contain the part of your string after the -.

这会将您的字符串分成两部分。数组中的第一个元素将是包含 . 之前-内容的部分,数组中的第二个元素将包含-.之后的字符串部分。

If the array length is not 2, then the string was not in the format: string-string.

如果数组长度不是 2,则字符串不是以下格式:string-string

Check out the split()method in the Stringclass.

查看类中的split()方法String

https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-int-

https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-int-

回答by secmask

String[] out = string.split("-");

should do thing you want. String class has many method to operate with string.

应该做你想做的事。String 类有很多操作字符串的方法。

回答by Michael Konietzka

The requirements left room for interpretation. I recommend writing a method,

要求留下了解释的空间。我建议写一个方法,

public final static String[] mySplit(final String s)

which encapsulate this function. Of course you can use String.split(..) as mentioned in the other answers for the implementation.

封装了这个函数。当然,您可以使用其他实现答案中提到的 String.split(..) 。

You should write some unit-tests for input strings and the desired results and behaviour.

您应该为输入字符串以及所需的结果和行为编写一些单元测试。

Good test candidates should include:

优秀的应试者应包括:

 - "0022-3333"
 - "-"
 - "5555-"
 - "-333"
 - "3344-"
 - "--"
 - ""
 - "553535"
 - "333-333-33"
 - "222--222"
 - "222--"
 - "--4555"

With defining the according test results, you can specify the behaviour.

通过定义相应的测试结果,您可以指定行为。

For example, if "-333"should return in [,333]or if it is an error. Can "333-333-33"be separated in [333,333-33] or [333-333,33]or is it an error? And so on.

例如,如果"-333"应该返回[,333]或者它是一个错误。可以"333-333-33"分开[333,333-33] or [333-333,33]还是报错?等等。

回答by Rob Hague

An alternative to processing the string directly would be to use a regular expression with capturing groups. This has the advantage that it makes it straightforward to imply more sophisticated constraints on the input. For example, the following splits the string into two parts, and ensures that both consist only of digits:

直接处理字符串的另一种方法是使用带有捕获组的正则表达式。这样做的优点是可以直接暗示对输入的更复杂的约束。例如,以下将字符串拆分为两部分,并确保两者都只包含数字:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

class SplitExample
{
    private static Pattern twopart = Pattern.compile("(\d+)-(\d+)");

    public static void checkString(String s)
    {
        Matcher m = twopart.matcher(s);
        if (m.matches()) {
            System.out.println(s + " matches; first part is " + m.group(1) +
                               ", second part is " + m.group(2) + ".");
        } else {
            System.out.println(s + " does not match.");
        }
    }

    public static void main(String[] args) {
        checkString("123-4567");
        checkString("foo-bar");
        checkString("123-");
        checkString("-4567");
        checkString("123-4567-890");
    }
}

As the pattern is fixed in this instance, it can be compiled in advance and stored as a static member (initialised at class load time in the example). The regular expression is:

由于模式在此实例中是固定的,因此可以提前编译并存储为静态成员(在示例中在类加载时初始化)。正则表达式为:

(\d+)-(\d+)

The parentheses denote the capturing groups; the string that matched that part of the regexp can be accessed by the Match.group() method, as shown. The \d matches and single decimal digit, and the + means "match one or more of the previous expression). The - has no special meaning, so just matches that character in the input. Note that you need to double-escape the backslashes when writing this as a Java string. Some other examples:

括号表示捕获组;与正则表达式的那部分匹配的字符串可以通过 Match.group() 方法访问,如图所示。\d 匹配单个十进制数字,+ 表示“匹配一个或多个前面的表达式。- 没有特殊含义,所以只匹配输入中的那个字符。请注意,您需要对反斜杠进行双重转义将其编写为 Java 字符串时。其他一些示例:

([A-Z]+)-([A-Z]+)          // Each part consists of only capital letters 
([^-]+)-([^-]+)            // Each part consists of characters other than -
([A-Z]{2})-(\d+)           // The first part is exactly two capital letters,
                           // the second consists of digits

回答by Mnyikka

// This leaves the regexes issue out of question
// But we must remember that each character in the Delimiter String is treated
// like a single delimiter        

public static String[] SplitUsingTokenizer(String subject, String delimiters) {
   StringTokenizer strTkn = new StringTokenizer(subject, delimiters);
   ArrayList<String> arrLis = new ArrayList<String>(subject.length());

   while(strTkn.hasMoreTokens())
      arrLis.add(strTkn.nextToken());

   return arrLis.toArray(new String[0]);
}

回答by SHUNMUGA RAJ PRABAKARAN

You can try like this also

你也可以这样试试

 String concatenated_String="hi^Hello";

 String split_string_array[]=concatenated_String.split("\^");

回答by SHUNMUGA RAJ PRABAKARAN

Sometimes if you want to split string containing +then it won't split; instead you will get a runtime error. In that case, first replace + to _and then split:

有时如果你想分裂,string containing +它就不会分裂;相反,您将获得一个runtime error. 在这种情况下,首先replace + to _然后拆分:

 this.text=text.replace("/", "_");
            String temp[]=text.split("_");

回答by Akhilesh Dhar Dubey

public class SplitTest {

    public static String[] split(String text, String delimiter) {
        java.util.List<String> parts = new java.util.ArrayList<String>();

        text += delimiter;

        for (int i = text.indexOf(delimiter), j=0; i != -1;) {
            String temp = text.substring(j,i);
            if(temp.trim().length() != 0) {
                parts.add(temp);
            }
            j = i + delimiter.length();
            i = text.indexOf(delimiter,j);
        }

        return parts.toArray(new String[0]);
    }


    public static void main(String[] args) {
        String str = "004-034556";
        String delimiter = "-";
        String result[] = split(str, delimiter);
        for(String s:result)
            System.out.println(s);
    }
}

回答by David

The fastest way, which also consumes the least resource could be:

消耗最少资源的最快方式可能是:

String s = "abc-def";
int p = s.indexOf('-');
if (p >= 0) {
    String left = s.substring(0, p);
    String right = s.substring(p + 1);
} else {
  // s does not contain '-'
}