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?
-
`String.isBlank()` will tell you if it's only a space or `""` – Patrick J Abare II Sep 24 '14 at 12:50
-
2`String.isBlank()` is an apache util method and not part of JDK – user1933888 Sep 24 '14 at 12:58
-
Possible duplicate of [How do I check that a Java String is not all whitespaces?](https://stackoverflow.com/questions/3247067/how-do-i-check-that-a-java-string-is-not-all-whitespaces) – Oleg Estekhin Sep 25 '17 at 12:40
5 Answers
Try the followoing :
if(str.trim().isEmpty()){
str = null;
}

- 3,054
- 1
- 20
- 37
-
1He said 'space', not 'whitespace'. `trim()` operates on whitespace. Subtle difference that may be important to him. – Andy Brown Sep 24 '14 at 12:53
-
-
1@AndyBrown could you give me the link or explanation here what are the difference between those 2 things in programming?) – Donvino Sep 24 '14 at 12:56
-
I also understand the question the way that he doesn't want empty strings. This also includes all other whitespace characters, too. – Rene M. Sep 24 '14 at 13:01
-
@Donvino you could google it. Basically, space is the 32 ascii char ' ', whitespaces are tab, new line, etc.. – DeiAndrei Sep 24 '14 at 13:01
-
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");
}

- 947
- 6
- 16
-
-
it works for multiple spaces but not for whitespaces (e.g. tab, newline) – DeiAndrei Sep 24 '14 at 12:59
-
use `spaces.matches("[ ]+")` to identify a string that is entirely composed of one or more spaces. – DwB Sep 24 '14 at 12:59
-
it works, now i have one more problem, if in my string variabel contains single/multiple of space and single/multiple of - character, ex:( - ), i want to replace with "null". – Muhammad Haryadi Futra Sep 24 '14 at 13:12
-
@Muhammad just replace the [ ] regex with [ -]. Note that + means 1 ore more, * stands for 0 or more – DeiAndrei Sep 24 '14 at 13:14
Suppose your variable is String var
Then,
if(var.replace(" ", "").equals("")) {
var = null;
}

- 1,348
- 4
- 19
- 37
First off all you can implement it your self for example by using a regular expression which is very simple.
The Java Regex definition defines "/s" as the pattern for all whitespace characters. So a String matching "/s+" is empty or only includes whitespaces.
Here is an example:
public boolean isEmpty(String value) {
return value.matches("/s*");
}
But normaly it isn't a good idea to do this by your self. It is a so common pattern that it is implemented in a lot of libraries already. My best practice in nearly all java apps I've written is to use the apache commons lang library. Which includes the StringUtils class. All methods in this class are nullsave and keep an eye on all possible scenarios about what is for example an empty string.
So with apache commons it is:
StringUtils.isBlank(value);
Have a look here: http://commons.apache.org/proper/commons-lang/javadocs/api-3.3.2/index.html

- 2,660
- 15
- 24
How about this?
if(yourstring.replace(" ","").length()==0) {
yourstring = null;
}
Doesn't need regexes so should be a little more efficient than solutions that do.

- 11,766
- 2
- 42
- 61