简体   繁体   English

分割字符串java

[英]Splitting a string java

I have a string in format: 我有一个格式字符串:

<+923451234567>: Hi here is the text. <+923451234567>:您好,这里是文本。

Now I want to get the mobile number(without any non-alphanumeric characters) ie 923451234567 in the start of the string in-between < > symbols, and also the text ie Hi here is the text. 现在,我想获取手机号(不包含任何非字母数字字符),即在<>符号之间的字符串开头的923451234567,以及文本,即Hi,这里是文本。

Now I can place a hardcoded logic, which I am currently doing. 现在,我可以放置一个当前正在执行的硬编码逻辑。

String stringReceivedInSms="<+923451234567>: Hi here is the text.";

String[] splitted = cpaMessage.getText().split(">: ", 2);
String mobileNumber=MyUtils.removeNonDigitCharacters(splitted[0]);
String text=splitted[1];

How can I neatly get the required strings from the string with regular expression? 如何从带有正则表达式的字符串中整齐地获取所需的字符串? So that I don't have to change the code whenever the format of the string changes. 这样我就不必在字符串格式更改时就更改代码。

String stringReceivedInSms="<+923451234567>: Hi here is the text.";

Pattern pattern = Pattern.compile("<\\+?([0-9]+)>: (.*)");
Matcher matcher = pattern.matcher(stringReceivedInSms);
if(matcher.matches()) {
    String phoneNumber = matcher.group(1);
    String messageText = matcher.group(2);
}

Use a regex that matches the pattern - <\\\\+?(\\\\d+)>: (.*) 使用匹配模式的正则表达式- <\\\\+?(\\\\d+)>: (.*)

Use the Pattern and Matcher java classes to match the input string. 使用PatternMatcher Java类来匹配输入字符串。

Pattern p = Pattern.compile("<\\+?(\\d+)>: (.*)");
Matcher m = p.matcher("<+923451234567>: Hi here is the text.");
if(m.matches())
{
    System.out.println(m.group(1));
    System.out.println(m.group(2));
}

You need to use regex, the following pattern will work: 您需要使用正则表达式,以下模式将起作用:

^<\\+?(\\d++)>:\\s*+(.++)$

Here is how you would use it - 这是您将如何使用它-

public static void main(String[] args) throws IOException {
    final String s = "<+923451234567>: Hi here is the text.";
    final Pattern pattern = Pattern.compile(""
            + "#start of line anchor\n"
            + "^\n"
            + "#literal <\n"
            + "<\n"
            + "#an optional +\n"
            + "\\+?\n"
            + "#match and grab at least one digit\n"
            + "(\\d++)\n"
            + "#literal >:\n"
            + ">:\n"
            + "#any amount of whitespace\n"
            + "\\s*+\n"
            + "#match and grap the rest of the string\n"
            + "(.++)\n"
            + "#end anchor\n"
            + "$", Pattern.COMMENTS);
    final Matcher matcher = pattern.matcher(s);
    if (matcher.matches()) {
        System.out.println(matcher.group(1));
        System.out.println(matcher.group(2));
    }
}

I have added the Pattern.COMMENTS flag so the code will work with the comments embedded for future reference. 我已经添加了Pattern.COMMENTS标志,因此该代码将与嵌入的注释一起使用,以备将来参考。

Output: 输出:

923451234567
Hi here is the text.

You can get your phone number by just doing : 您只需执行以下操作即可获取电话号码:

stringReceivedInSms.substring(stringReceivedInSms.indexOf("<+") + 2, stringReceivedInSms.indexOf(">"))

So try this snippet: 因此,请尝试以下代码段:

public static void main(String[] args){
        String stringReceivedInSms="<+923451234567>: Hi here is the text.";

        System.out.println(stringReceivedInSms.substring(stringReceivedInSms.indexOf("<+") + 2, stringReceivedInSms.indexOf(">")));
    }

You don't need to split your String. 您不需要拆分字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM