How to know if the string variabel contains only space in java?
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? Try the followoing : if(str.trim().isEmpty()){ str = null; } Suppose your variable is String var Then, if(var.replace(" ", "").equals("")) { var = null; } 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"); } How about this? if(yourstring.replace(" ","").length()==0) { yourstring = null; } Doesn't need