简体   繁体   English

我在Java中有“”时如何返回null?

[英]How to return null when I have “ ” in java?

I am coming for a help where I have a json object and return a string and few of my strings are return as " " meaning there is a empty value but there is a space in the quotation. 我正在寻求帮助,我有一个json对象并返回一个字符串,而我的几个字符串都返回为" "这意味着有一个空值,但引号中有一个空格。 So, what I want to accomplish is I need help to check or implement a way to show null value instead of " " . 因此,我想完成的工作是我需要检查或实施一种显示null值而不是" " is there a way with my code below? 下面的代码有办法吗? thanks for the help. 谢谢您的帮助。

A simple method that trims the String (removes leading/trailing whitespace) and then checks if the String is empty should do the trick: 一个简单的方法可以修整String(除去开头/结尾的空白),然后检查String是否为空,可以解决这个问题:

public String getTrimmedString(String str) {
    if (str.trim().isEmpty()) {
        return null;
    } else {
        return str.trim();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM