Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.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 如何在n“之后获取字符串-&引用;(破折号)在爪哇?_Java_String - Fatal编程技术网

Java 如何在n“之后获取字符串-&引用;(破折号)在爪哇?

Java 如何在n“之后获取字符串-&引用;(破折号)在爪哇?,java,string,Java,String,据推测,我有一个名为c的字符串,其中存储了“BPI-1111-2203-4493” String c = "BPI-1111-2203-4493"; 现在,我想将此字符串拆分为两个字符串,如下所示: String one = "BPI"; String two = "1111-2203-4493"; String one = "BPI"; String two = "1111"; 我试着用这个: String[] b = bank.split("-"); String one = b[0

据推测,我有一个名为c的字符串,其中存储了“BPI-1111-2203-4493”

String c = "BPI-1111-2203-4493";
现在,我想将此字符串拆分为两个字符串,如下所示:

String one = "BPI";
String two = "1111-2203-4493";
String one = "BPI";
String two = "1111";
我试着用这个:

String[] b = bank.split("-"); 
String one = b[0];
String two = b[1];
但是字符串的结尾是这样的:

String one = "BPI";
String two = "1111-2203-4493";
String one = "BPI";
String two = "1111";
我应该使用什么字符串函数,以便字符串2将具有“1111-2203-4493”,而不仅仅是“1111”


谢谢你的帮助

您可以使用limit进行拆分

String c = "BPI-1111-2203-4493";
String[] s = c.split("-", 2);

你可以有限制地拆分

String c = "BPI-1111-2203-4493";
String[] s = c.split("-", 2);

一个简单的解决方案是使用
子字符串
索引of
方法,如下所示:

String c = "BPI-1111-2203-4493";
String one = c.subString(0, c.indexOf('-'));
String two = c.subString(c.indexOf('-') + 1, c.length());

一个简单的解决方案是使用
子字符串
索引of
方法,如下所示:

String c = "BPI-1111-2203-4493";
String one = c.subString(0, c.indexOf('-'));
String two = c.subString(c.indexOf('-') + 1, c.length());

要在第n个破折号后拆分,请使用正则表达式查找要拆分的点,并调用
substring()
。只需将
{}
之间的数字更改为适当的n值

(?:[^-]*-){2}
以下是所有3个n的演示:

String input = "BPI-1111-2203-4493";
Matcher m;

// Split on 1st dash
m = Pattern.compile("(?:[^-]*-){1}").matcher(input);
if (m.find())
    System.out.printf("%s, %s%n", input.substring(0, m.end() - 1),
                                  input.substring(m.end()));

// Split on 2nd dash
m = Pattern.compile("(?:[^-]*-){2}").matcher(input);
if (m.find())
    System.out.printf("%s, %s%n", input.substring(0, m.end() - 1),
                                  input.substring(m.end()));

// Split on 3rd dash
m = Pattern.compile("(?:[^-]*-){3}").matcher(input);
if (m.find())
    System.out.printf("%s, %s%n", input.substring(0, m.end() - 1),
                                  input.substring(m.end()));
输出:

BPI,1111-2203-4493
BPI-11112203-4493
BPI-1111-22034493

可以轻松转换为可重用的帮助器方法:

private static String[] split(String input, char ch, int n) {
    if (n <= 0)
        throw new IllegalArgumentException("n must be >0");
    Matcher m = Pattern.compile("(?:[^" + ch + "]*" + ch + "){" + n + "}").matcher(input);
    if (! m.find())
        return new String[] { input };
    return new String[] { input.substring(0, m.end() - 1),
                          input.substring(m.end()) };
}
输出:

[BPI,1111-2203-4493]
[BPI-11112203-4493]
[BPI-1111-22034493]
[BPI-1111-2203-4493]

或者您可以遵循并使用:

私有静态字符串[]拆分(字符串输入,字符ch,int n){

如果(n要在第n个破折号后拆分,请使用正则表达式找到要拆分的点,并调用
substring()
。只需更改
{}
之间的数字以获得适当的n值

(?:[^-]*-){2}
以下是所有3个n的演示:

String input = "BPI-1111-2203-4493";
Matcher m;

// Split on 1st dash
m = Pattern.compile("(?:[^-]*-){1}").matcher(input);
if (m.find())
    System.out.printf("%s, %s%n", input.substring(0, m.end() - 1),
                                  input.substring(m.end()));

// Split on 2nd dash
m = Pattern.compile("(?:[^-]*-){2}").matcher(input);
if (m.find())
    System.out.printf("%s, %s%n", input.substring(0, m.end() - 1),
                                  input.substring(m.end()));

// Split on 3rd dash
m = Pattern.compile("(?:[^-]*-){3}").matcher(input);
if (m.find())
    System.out.printf("%s, %s%n", input.substring(0, m.end() - 1),
                                  input.substring(m.end()));
输出:

BPI,1111-2203-4493
BPI-11112203-4493
BPI-1111-22034493

可以轻松转换为可重用的帮助器方法:

private static String[] split(String input, char ch, int n) {
    if (n <= 0)
        throw new IllegalArgumentException("n must be >0");
    Matcher m = Pattern.compile("(?:[^" + ch + "]*" + ch + "){" + n + "}").matcher(input);
    if (! m.find())
        return new String[] { input };
    return new String[] { input.substring(0, m.end() - 1),
                          input.substring(m.end()) };
}
输出:

[BPI,1111-2203-4493]
[BPI-11112203-4493]
[BPI-1111-22034493]
[BPI-1111-2203-4493]

或者您可以遵循并使用:

私有静态字符串[]拆分(字符串输入,字符ch,int n){

如果(n您的问题是,在您的情况下,split方法将返回一个包含四项的数组:

b[0] = "BPI"
b[1] = "1111"
b[2] = "2203"
b[3] = "4493"
我建议对前面的一些答案采用类似的方法,但首先要进行一些错误检查:

String c = "BPI-1111-2203-4493";
String one = null;
String two = null;
if((null != c)) {
    int firstDashIndex = c.indexOf('-');
    if(firstDashIndex >= 0) {
        one = c.substring(0, firstDashIndex);
        if((firstDashIndex + 1) < c.length()) {
            two = c.substring(firstDashIndex + 1);
        }
    }
}
System.out.println(one);
System.out.println(two);
String c=“BPI-1111-2203-4493”;
字符串1=null;
字符串2=null;
如果((null!=c)){
int firstDashIndex=c.indexOf('-');
如果(firstDashIndex>=0){
一个=c.子字符串(0,第一个索引);
如果((firstDashIndex+1)

希望有帮助。

您的问题是,在您的情况下,split方法将返回一个包含四项的数组:

b[0] = "BPI"
b[1] = "1111"
b[2] = "2203"
b[3] = "4493"
我建议对前面的一些答案采用类似的方法,但首先要进行一些错误检查:

String c = "BPI-1111-2203-4493";
String one = null;
String two = null;
if((null != c)) {
    int firstDashIndex = c.indexOf('-');
    if(firstDashIndex >= 0) {
        one = c.substring(0, firstDashIndex);
        if((firstDashIndex + 1) < c.length()) {
            two = c.substring(firstDashIndex + 1);
        }
    }
}
System.out.println(one);
System.out.println(two);
String c=“BPI-1111-2203-4493”;
字符串1=null;
字符串2=null;
如果((null!=c)){
int firstDashIndex=c.indexOf('-');
如果(firstDashIndex>=0){
一个=c.子字符串(0,第一个索引);
如果((firstDashIndex+1)

希望有帮助。

indexOf
substring
?@JonSkeet如果我知道哪个
-
,那就很容易了。但是“第n个
-
”怎么办?你可以多次调用
indexOf
,每次都传递上一次调用的结果以继续。(看看带初始索引的
indexOf
重载。)
indexOf
substring
?@jonsket如果我知道哪个
-
,那就很容易了。但是“第n个
-
”怎么办呢?你可以多次调用
indexOf
,每次都传递上一次调用的结果以继续。(看看带初始索引的
indexOf
的重载。)问题是如何在第n个破折号上拆分,例如,n=2表示
BPI-1111
2203-4493
@Andreas,然后你就可以执行
c拆分(“-”,3)[2]
这将给你
2203-4493
的问题是如何在第n个破折号上拆分,例如,n=2表示
BPI-1111
2203-4493
@Andreas,然后你就可以执行
c拆分(“-”,3)[2]
这将给您提供
2203-4493
问题是如何在第n个破折号上拆分,例如n=2表示
BPI-1111
2203-4493
。问题是如何在第n个破折号上拆分,例如n=2表示
BPI-1111
2203-4493