简体   繁体   English

格式化字符串:删除电话号码

[英]Formatting a String : To remove the telephone number

I'm having a String(StringTo) such as 我有一个String(StringTo) ,如

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 ]

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

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