在java中如何区分数字和字符串?

在java中如何区分数字和字符串?,java,regex,Java,Regex,我有一个由字符和整数组成的字符串。例如 Business Call Numbers: US Toll Free 1-009-123-8924 UK Toll Free 08081234567 India Toll Free 180012345678 Chennai Number 044222 Bangalore Number 0802214434 Conference Bridge 4542316572 现在我想免费获取数字并将其存储在一个数组中,将其他数字存储在另一个数组中。我知道这很容易做

我有一个由字符和整数组成的字符串。例如

Business Call Numbers:
US Toll Free 1-009-123-8924
UK Toll Free 08081234567
India Toll Free 180012345678
Chennai Number 044222
Bangalore Number 0802214434
Conference Bridge 4542316572
现在我想免费获取数字并将其存储在一个数组中,将其他数字存储在另一个数组中。我知道这很容易做到,但我不知道使用哪种方法?正则表达式(indexof()、contains()、matches()等)


任何人都可以分享你的经验吗?

根据你的意见

尝试使用
lastIndexOf(“”)方法

它返回您所需的最后一个空间的位置

然后你可以得到下一个字符串,这是你的电话号码

public class Test {
public static void main(String[] args) {
    String str1 = "US Toll Free 1-009-123-8924";

    int pos = str1.lastIndexOf(" ");
    String s[] = str1.subString(pos); //kind of
    //your s[1] is what you need 
}
}

免费后的电话号码

 String str1 = "US Toll Free 1-009-123-8924  UK Toll Free 111  UK Toll sime 222 India Toll Free 333 ";
        String numPattern = "(toll free )\\d[-\\d]*\\d";

        Pattern pattern = Pattern.compile(numPattern);
        Matcher matcher = pattern.matcher(str1.toLowerCase());
        while (matcher.find()) {
            System.out.println(matcher.group().replace("toll free ", ""));
        }

您可以使用Pranalee所示的模式/匹配器,但是使用这个正则表达式“[0-9-]+”

使用示例数字,看起来数字中没有空格,而且也在一行的末尾,因此您可以使用.split(“”),它将使用空格作为分隔符

String a = "US Toll Free 1-009-123-8924";
String[] b = a.split(" ");
String number = b[b.length-1];
if(b.length>2){
    String sToCheck = b[b.length-3] + " " + b[b.length-2];
    if(sToCheck.equals("Toll Free")){
        //Add to the toll free array
    }
}else{
    //Add to the non toll free array
}
试试看


您能举个例子吗?这里的子字符串是什么?请查看java文档。你会得到答案,问题是:“我想在免费后得到这些号码”如果像英国免费电话08081234567 ABC这样的号码,那么它也会包括ABC工作正常,但我的所有号码都会在一个字符串中。如何继续?不,它不应该包括ABC。我想要免费后的号码。有可能吗?Masudhave已修改为单个字符串。希望此帮助能让您简要解释一下,如果该号码是“美国免费1-009-123-8924美国免费1-009-123-8924”,您将如何获得该号码?
String a = "US Toll Free 1-009-123-8924";
String[] b = a.split(" ");
String number = b[b.length-1];
if(b.length>2){
    String sToCheck = b[b.length-3] + " " + b[b.length-2];
    if(sToCheck.equals("Toll Free")){
        //Add to the toll free array
    }
}else{
    //Add to the non toll free array
}
public static void main(String[] args) {
        String str1 = "US Toll Free 1-009-123-8924 ABC";
        String str2 = "UK Toll Free 08081234567 ABC";
        String str3 = "India Toll Free 180012345678";
        String str4 = "India Toll Free 180012345678";

        if(str1.contains("Toll Free") || str1.contains("store") ){
           //Store it one array
        }
        else{
          //Use other array.
        }   
        System.out.println(getMatch(str1));
        System.out.println(getMatch(str2));
        System.out.println(getMatch(str3));
        System.out.println(getMatch(str4));

    }

    private static String getMatch(String input) {
       return input.replaceAll("[a-zA-Z ]+","");
    }