How to know if the string variabel contains only space in java?

前端 未结 5 1249
时光取名叫无心
时光取名叫无心 2020-12-22 01:30

I have a variable that\'s a string and I want to replace the string with \"null\" if the variable contains only a space or multiple spaces. How can I do it?

5条回答
  •  情深已故
    2020-12-22 02:21

    This is a way you could do it:

        String spaces = "   -- - -";
        if (spaces.matches("[ -]*")) {
            System.out.println("Only spaces and/or - or empty");
        }
        else {
            System.out.println("Not only spaces");
        }
    

提交回复
热议问题