Replace alphabet in a string using replace?

前端 未结 4 1613
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-19 14:20

I\'m wondering if I can use string.replace() to replace all alphabets in a string?

String sentence = \"hello world! 722\"
String str = sentence         


        
4条回答
  •  执笔经年
    2020-12-19 14:21

    You can use the following (regular expression):

        String test = "hello world! 722";
        System.out.println(test);
        String testNew = test.replaceAll("(\\p{Alpha})", "@");
        System.out.println(testNew);
    

    You can read all about it in here: http://docs.oracle.com/javase/tutorial/essential/regex/index.html

提交回复
热议问题