简体   繁体   中英

Formatting a String : To remove the telephone number

I'm having a String(StringTo) such as

John [0775678899] ,Ann [0776789988] ,Mary [0777789900] 

But I need to format the string to display it as:

John, Ann, Mary

I tried using the below method but it didn't give me the expected result:

StringTo = StringTo.replace("//[^10] ,/", ", ");

Can someone tell me whats wrong or any other way to compute this?

用空字符串替换正则表达式\\[\\d+\\]

Try this, it will format the white-space properly:

StringTo = StringTo.replaceAll("\\s*\\[\\d+\\]\\s*(,?)", "$1 ");

Input:

John [0775678899] ,Ann [0776789988] ,Mary [0777789900] 

Output:

John, Ann, Mary 

If I understood you correctly - you need to remove all numbers inside [] . Try this:

StringTo.replaceAll("\\[.*?\\] ","");
public static void main(String[] args) {
    String s = "John [0775678899] ,Ann [0776789988] ,Mary [0777789900]";
    String[] ss = s.split(",");
    ArrayList<String >name = new ArrayList<String>();
    for(int i = 0; i< ss.length; i++){
        String[]splitted = ss[i].split("\\[");
        name.add(splitted[0]);
    }
    System.out.println(name);
}

Output:

[John , Ann , Mary ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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