Java 在一个语句中用新值替换空字段、空字段和仅空白字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20389284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:35:53  来源:igfitidea点击:

Replace empty, null and whitespace-only fields with new value in one statement

javatalend

提问by Drewdavid

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 onlywhite spaces

我有一个 Java 表达式,我在 Talend Open Studio 的 tMap 组件中使用它,试图清理空字段、空字段且包含空格

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

(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?

我从这里借用了空格语句:How do I check that a Java String is not all whitespaces?

采纳答案by dev2d

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

或者尝试StringHandling.TRIM(myTable.myField).equals(""),或者StringHandling.TRIM(myTable.myField).isEmpty()检查这个

回答by Josiah Hester

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

会打印出来 teffaadd

回答by Mike Strobel

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 trueif the value is null, empty, or consists only of whitespace. For your use case, you could write:

true如果值为null、为空或仅包含空格,则该方法返回。对于您的用例,您可以编写:

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

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

与使用 的解决方案不同trim(),这不会产生任何垃圾。

回答by ren78min

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

如果您不介意导入 jar,apache commons可以解决它。

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

否则,String 本身有一个 trim() 方法,它将帮助您:

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;
    }
}
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;
    }
}