1

I have a Java expression that's I'm using in Talend Open Studio's tMap component, trying to clean up fields that are null, empty and consist of only white spaces

I tried this statement but it hasn't remove the white spaces:

(myTable.myField == null || StringHandling.TRIM(myTable.myField).length() > 0 || myTable.myField.isEmpty()) ? "Not Set" : myTable.myField

The nulls and empty fields are working, but I have a field with just a white space in it and this isn't getting rid of it

I'd like to remove all spaces from , not just leading/trailing

I borrowed the whitespace statement from here: How do I check that a Java String is not all whitespaces?

Community
  • 1
  • 1
Drewdavid
  • 3,071
  • 7
  • 29
  • 53
  • http://stackoverflow.com/questions/4576352/java-remove-all-occurances-of-char-from-string dupe, please do some research before asking a question. – fonZ Dec 05 '13 at 00:57
  • Hi, does the question you have linked to also deal with null values? – Drewdavid Dec 05 '13 at 18:55

4 Answers4

1

i think you have made a typo just change

(myTable.myField == null || StringHandling.TRIM(myTable.myField).length() > 0 || myTable.myField.isEmpty()) ? "Not Set" : myTable.myField

to

(myTable.myField == null || StringHandling.TRIM(myTable.myField).length() == 0 || myTable.myField.isEmpty()) ? "Not Set" : myTable.myField

or try StringHandling.TRIM(myTable.myField).equals("") either, or StringHandling.TRIM(myTable.myField).isEmpty() check this

dev2d
  • 4,245
  • 3
  • 31
  • 54
  • Thank You; I ended up not needing the third case, since I think StringHandling.TRIM(myTable.myField).length()==0 should also catch the empty strings – Drewdavid Dec 05 '13 at 23:11
0

I have a utility method that I use:

public static boolean isNullOrWhiteSpace(final CharSequence value) {
    if (value == null) {
        return true;
    }

    final int length = value.length();

    for (int i = 0; i < length; i++) {
        if (!Character.isWhitespace(value.charAt(i))) {
            return false;
        }
    }

    return true;
}

The method returns true if the value is null, empty, or consists only of whitespace. For your use case, you could write:

String result = isNullOrWhiteSpace(myTable.myField) ? "Not Set"
                                                    : myTable.myField;

Unlike solutions that use trim(), this will not generate any garbage.

Mike Strobel
  • 25,075
  • 57
  • 69
0

If you don't mind importing a jar, apache commons can solve it.

Otherwise, then String itself has a trim() method, which will help you:

public class ReplaceEmpty {
    public static void main(String[] args) throws Exception {
        String[] ss = { "", "   ", null , "GOOD String"};
        for (int i = 0; i < ss.length; i++) {
            System.out.println(ReplaceEmpty.process(ss[i]));
        }
    }

    public static String process(String s) {
        return (s == null || s.trim().equals("")) ? "REPLACEMENT" : s;
    }
}

ren78min
  • 66
  • 6
-1

Use this to replace all the spaces in your string with the empty string.

myTable.myField = myTable.myField.replaceAll(" ", "");

For example this:

String test = "t eff  aa d d ";
System.out.println(test.replaceAll(" ", ""));   

Will print out teffaadd

Josiah Hester
  • 6,065
  • 1
  • 24
  • 37
  • 1
    Hi, this deals with the blank spaces, but what about nulls and empty fields? I'd like to give treat them all alike and give them a default value like "invalid" or "not set" – Drewdavid Dec 05 '13 at 18:57