如何在Java中拆分字符串 [英] How to split a string in Java

查看:28
本文介绍了如何在Java中拆分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,"004-034556",我想把它分成两个字符串:

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

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?

推荐答案

只要使用合适的方法:String#split().

Just use the appropriate method: 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 characters if necessary.

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

there are 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".

因此,如果您想拆分,例如period/dot . 表示正则表达式中的任何字符",使用 反斜杠 来转义单个特殊字符,如所以split("\."),或者使用字符类[] 像这样表示文字字符 split("[.]"),或者使用 Pattern#quote() 像这样转义整个字符串 split(Pattern.quote(".")).

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(".")).

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

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

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

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

注意,这不需要正则表达式.为此,请使用 String#matches() 代替.

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

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

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

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

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

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

这篇关于如何在Java中拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆