简体   繁体   English

如何在java hsa控制台中删除两个字符串之间的空格?

[英]How do you remove spaces between two strings in java hsa console?

public class TestParse
{
    static Console c;           // The output console

    public static void main (String[] args)
    {
        c = new Console ();
        String userNum = c.readString ();
        String userNum2 = userNum.replaceAll ("\\p{Z}","");
        c.println (userNum2);

   } // main method
} // TestParse class

The input is supposed to be one digit followed by a space and then another digit. 输入应该是一个数字,后跟一个空格,然后是另一个数字。 Every time I try that, it only prints the first digit. 每次尝试时,它只会打印第一位。 How do I fix that? 我该如何解决? It should print the two digits without a space between them. 它应该打印两个数字,并且两者之间没有空格。 I already tried using st.replaceAll("\\\\s+","") and st.replaceAll("\\\\s","") 我已经尝试使用st.replaceAll("\\\\s+","")st.replaceAll("\\\\s","")

Try this: 尝试这个:

String userNum2 = unserNum.replaceAll("( )+", "");

Or 要么

String userNum2 = unserNum.replaceAll(" ", "");

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

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