字符串 - 查找空值

时间:2013-12-13 07:20:58

标签: java string null

我需要在字符串中找到空值。例如

String x= "isPay=false, isAlreadyRented=false, differenceBetweenStartTimeandNow=, thumbnailUrl=http://";
char strline2[] = x.toCharArray();
for (i = pos + 50; strline2[i] != '=' && i < strline2.length - 1; i++)
    ;
i = i + 1;
for (i = i; i < strline2.length - 284; i++) {
    if (strline2[i] == ',') {
        for (i = i; strline2[i] != '=' && i < strline2.length - 1; i++)
            ;
        i = i + 1;
        br_output.write("\t");
    }
    if (strline2[i] != ',' && strline2[i] != '=')
        br_output.write(strline2[i]);
}

从此我遍历到=,提取=,之间的值并将其存储在文件中。因此,当没有值时,例如differenceBetweenStartTimeandNow=,,它应该在文件中写为NULL。任何人都可以帮助我。

3 个答案:

答案 0 :(得分:0)

尝试

if(strline2[i] == '=' && strline2[i+1] == ','){
   r_output.write("NULL");
   continue
}

答案 1 :(得分:0)

尝试:

String x= "isPay=false, isAlreadyRented=false, differenceBetweenStartTimeandNow=, thumbnailUrl=http://";
x = x.replaceAll("=,",  "=NULL,");

这将替换所有空值,即=,with = NULL,然后您可以继续提取值并将其存储在文件中的逻辑

我希望这会有所帮助

答案 2 :(得分:0)

使用split,如下所示:

        String[] strline2 = x.split("=*,");

        for (int i = 0; i < strline2.length; i++)
        {
            int index = strline2[i].indexOf('=');
            if(index < 0)
                br_output.write("NULL");                
            else
                br_output.write(strline2[i].substring(index+1, strline2[i].length()));
        }