Get empty string when null

java convert empty string to null
how to check string array is null or empty in java
java 8 string null or empty
java check if string is empty or whitespace
sql insert empty string instead of null
get null when empty string java
string is null or empty
sql convert null to empty string

I want to get string values of my fields (they can be type of long string or any object),

if a field is null then it should return empty string, I did this with guava;

nullToEmpty(String.valueOf(gearBox))
nullToEmpty(String.valueOf(id))
...

But this returns null if gearbox is null! Not empty string because valueOf methdod returns string "null" which leads to errors.

Any Ideas?

EDIt: there are 100s fields I look for something easy to implement

You can use Objects.toString() (standard in Java 7):

Objects.toString(gearBox, "")

Objects.toString(id, "")

From the linked documentation:

public static String toString(Object o, String nullDefault)

Returns the result of calling toString on the first argument if the first argument is not null and returns the second argument otherwise.

Parameters: o - an object nullDefault - string to return if the first argument is null

Returns: the result of calling toString on the first argument if it is not null and the second argument otherwise.

See Also: toString(Object)

Convert null string to "", Convert null string to "" [duplicate] · java null. This question already has answers here: Get empty string when null  There is an excellent SO on MySQL empty strings versus NULL here, MySQL, better to insert NULL or empty string?, however it doesn't take in to account 'uniformity' - i.e. if you want to have just one choice in your tables (i.e. empty string OR NULL),

For java 8 you can use Optional approach:

Optional.ofNullable(gearBox).orElse("");
Optional.ofNullable(id).orElse("");

Check if a string is empty or null in Java, isEmpty() method to check for an empty string in Java. To avoid NullPointerException in case the string is null, the method call should be preceded by a null  OP was asking for "how to check for an empty string", un undefined variable is not an empty string. Anyway you could check typeof variable != "undefined" before checking if is empty. – Abimael Martell Apr 1 '14 at 18:56

If you don't mind using Apache commons, they have a StringUtils.defaultString(String str) that does this.

Returns either the passed in String, or if the String is null, an empty String ("").

If you also want to get rid of "null", you can do:

StringUtils.defaultString(str).replaceAll("^null$", "")

or to ignore case:

StringUtils.defaultString(str).replaceAll("^(?i)null$", "")

On Null or Why Empty Strings are not Same as Null, Empty string or Null. For strings things get a bit more complicated. A SQL NVARCHAR() NULL can be either empty or null . If you allow  An empty string has a Length of 0. The following example creates an empty string and displays its value and its length. String^ s = ""; Console::WriteLine ("The length of ' {0}' is {1}.", s, s->Length); // The example displays the following output: // The length of '' is 0.

Use an inline null check

gearBox == null ? "" : String.valueOf(gearBox);

Is it better to return NULL or empty values from functions/methods , An empty object implies data has been returned, whereas returning null clearly Personally, I like to return empty strings for functions that return strings to Result = result; } public bool Found { get; private set; } // Only valid if Found is true​  A string can be empty or have a null value. If a string is null, it isn't referring to anything in memory. Try s.length()>0. This is because if a string is empty, it still returns a length of 0. So if you enter nothing for the same, then it will still continue looping since it doesn't register the string as null.

Since you're using guava:

Objects.firstNonNull(gearBox, "").toString();

When to use NULL and when to use an empty string?, NULL varchars are treated as empty strings. I generally prefer NULL so that doing INSERT statements don't get tedious with forgetting to set  if a field is null then it should return empty string, I did this with guava; nullToEmpty(String.valueOf(gearBox)) nullToEmpty(String.valueOf(id)) But this returns null if gearbox is null! Not empty string because valueOf methdod returns string "null" which leads to errors.

How to replace NULL with Empty String in SQL Server , ISNULL() vs COALESCE() Examples. We often need to replace NULL values with empty String or blank in SQL e.g. while concatenating String. In  You can use the expression editor to set the value as null and it will empty that variable: Hope this Helps! If this reply has answered your question or solved your issue, please mark this question as answered. Answered questions helps users in the future who may have the same issue or question quickly find a resolution via search.

Transforming Empty Strings into Empty Optionals, In this quick tutorial, we'll present different ways to transform a null or empty String into an empty Optional. Getting an empty Optional out of null  In MySQL, an empty string and a NULL value have different meaning and usage. In this article, we discuss when to use NULL values over an empty string, and the appropriate operators for both. We will look at sample data and step-by-step examples to learn more about them.

Right way to check if String is empty in Java, String nullString = null; String empty = new String(); boolean test = "".equals(​empty); // true System.out.println(test); boolean check =  Not necessarily. An empty string is either not initialised or can only consist of a null terminator. An empty string is not a substring since it has no value. The size will be 0.Meaning there is no character assigned to it.

Comments
  • @Spring You should consider upgrading, especially with Java 8 coming out soon. In any case, I'll leave this answer in case it can be helpful for future visitors.
  • Downvoter: please leave a comment. I would like to know what is wrong with my answer, and a downvote alone doesn't tell me.
  • NIce. I suggest Objects.toString(id, "").trim()
  • why it requires > API 19... its a simple check. i guess i have to write that my self
  • The risk is that u r losing the type of your string. It is currently a String but if it will change to a different type in the future he compiler won't protect u
  • if id is not a string, it seems that the second line code cannot work
  • what if string is string "null"? thats my problem
  • @Spring: Updated to take care of that case too.
  • Two seconds too fast!
  • @Samhain there are 100s fields I look for something easy to implkement
  • Is that you in your profile picture? Isn't that Jon?
  • from guava 18 you need to use MoreObjects.firstNonNull
  • new String() is a waste of resources. Just use "".