如何从String,Android / Java获取多子字符串

时间:2023-01-21 15:19:26

I know there are similar questions regarding to this. However, I tried many solutions and it just does not work for me.

我知道对此有类似的问题。但是,我尝试了很多解决方案,它对我不起作用。

I need help to extract multiple substrings from a string:

我需要帮助从字符串中提取多个子字符串:

String content = "Ben Conan General Manager 90010021 benconan@gmail.com";

Note: The content in the String may not be always in this format, it may be all jumbled up.

注意:字符串中的内容可能并不总是采用这种格式,可能都是混乱的。

I want to extract the phone number and email like below:

我想提取电话号码和电子邮件,如下所示:

1. 90010021

2. benconan@gmail.com

In my project, I was trying to get this result and then display it into 2 different EditText. I have tried using pattern and matcher class but it did not work.

在我的项目中,我试图获得此结果,然后将其显示为2个不同的EditText。我尝试过使用模式和匹配器类,但它没有用。

I can provide my codes here if requested, please help me ~

如果需要,我可以在这里提供我的代码,请帮帮我〜

--------------------EDIT---------------------

Below is my current method which only take out the email address:

以下是我目前只取出电子邮件地址的方法:

private static final String EMAIL_PATTERN =
            "[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
                        "\\@" +
                        "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
                        "(" +
                            "\\." +
                            "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
                        ")+";



public String EmailValidator(String email) {

        Pattern pattern = Pattern.compile(EMAIL_PATTERN);
        Matcher matcher = pattern.matcher(email);

        if (matcher.find()) {

            return email.substring(matcher.start(), matcher.end());

        } else {

            // TODO handle condition when input doesn't have an email address

        }

        return email;
    }

4 个解决方案

#1


2  

You can separate your string into arraylist like this

你可以将你的字符串分成这样的arraylist

    String str = "Ben Conan, General Manager, 90010021, benconan@gmail.com";
List<String> List = Arrays.asList(str.split(" "));

#2


0  

maybe you should do this instead of yours :

也许你应该这样做而不是你的:

String[] Stringnames = new String[5]

Stringnames [0] = "your phonenumber"
Stringnames[1] = "your email"

System.out.println(stringnames)

Or :

String[] Stringnames = new String[2]
String[] Stringnames = {"yournumber","your phonenumber"};

System.out.println(stringnames [1]);

#3


0  

String.split(...) is a java method for that.

String.split(...)是一个java方法。

EXAMPLE:

String content = "Ben Conan, General Manager, 90010021, benconan@gmail.com";
String[] selection = content.split(",");
System.out.println(selection[0]);
System.out.println(selection[3]);

BUT if you want to do a Regex then take a look at this:

但如果你想做一个正则表达式,那么看看这个:

https://*.com/a/16053961/982161

#4


0  

Try this regex for phone number

试试这个正则表达式的电话号码

[\d+]{8}  ---> 8 represents number of digits in phone number

You can use

您可以使用

[\d+]{8,} ---> if you want the number of more than 8 digits

Use appropriate JAVA functions for matching. You can try the results here http://regexr.com/

使用适当的JAVA函数进行匹配。你可以在这里试试结果http://regexr.com/

For email, it depends whether the format is simple or complicated. There is a good explanation here

对于电子邮件,这取决于格式是简单还是复杂。这里有一个很好的解释

http://www.regular-expressions.info/index.html

#1


2  

You can separate your string into arraylist like this

你可以将你的字符串分成这样的arraylist

    String str = "Ben Conan, General Manager, 90010021, benconan@gmail.com";
List<String> List = Arrays.asList(str.split(" "));

#2


0  

maybe you should do this instead of yours :

也许你应该这样做而不是你的:

String[] Stringnames = new String[5]

Stringnames [0] = "your phonenumber"
Stringnames[1] = "your email"

System.out.println(stringnames)

Or :

String[] Stringnames = new String[2]
String[] Stringnames = {"yournumber","your phonenumber"};

System.out.println(stringnames [1]);

#3


0  

String.split(...) is a java method for that.

String.split(...)是一个java方法。

EXAMPLE:

String content = "Ben Conan, General Manager, 90010021, benconan@gmail.com";
String[] selection = content.split(",");
System.out.println(selection[0]);
System.out.println(selection[3]);

BUT if you want to do a Regex then take a look at this:

但如果你想做一个正则表达式,那么看看这个:

https://*.com/a/16053961/982161

#4


0  

Try this regex for phone number

试试这个正则表达式的电话号码

[\d+]{8}  ---> 8 represents number of digits in phone number

You can use

您可以使用

[\d+]{8,} ---> if you want the number of more than 8 digits

Use appropriate JAVA functions for matching. You can try the results here http://regexr.com/

使用适当的JAVA函数进行匹配。你可以在这里试试结果http://regexr.com/

For email, it depends whether the format is simple or complicated. There is a good explanation here

对于电子邮件,这取决于格式是简单还是复杂。这里有一个很好的解释

http://www.regular-expressions.info/index.html