2013-05-03 99 views
0

我有其中存储对象的数组列表,并从这些对象的吸气剂我得到字符串值如下所示修整字符串加检查,如果字符串不是空

List<abcd> hgfer = (List<abcd>)Getter.rows(jhfile); 
      for(abcd f: hgfer) 
      {   
      String p = f.getFromArea() 

作为数组列表上面所示和我正在提取的值。现在我必须确保,我得到的字符串不是空的,再加上它应修剪,我实现了这个,如下图所示:

p.getFromArea().trim().length() > 0 

现在有附加到该对象将返回一个字符串的几个干将。 对于每个单独的字符串,我必须这样做。我想使这将返回一个布尔值和字符串参数的单独个体的方法 将pass.For例如:

private String validaterow(String g) 
    { 
    boolean valid = false;' 
    try{ 
    **//code to check that should not be empty plus it should be trim one** 
    } 
    catch(){} 
    valid = false; 
    } 

,我必须使课堂内某处调用此方法

List<abcd> hgfer = (List<abcd>)Getter.rows(jhfile); 
      for(abcd f: hgfer) 
      {   
      if (!validaterow(f.getFromArea()) 
      {//customised message 
      } 
      else 
      continue; 

现在请各位指教我怎么能做到这一点的字符串不应该是空的,再加上它 应该是装饰一个

回答

0

试试这个:

private boolean validaterow(String text) 
    {  
    try{ 
     return (text == null) || (text != null && text.trim().length() == 0); 
    } 
    catch(){} 
     return false; 
    } 
    } 
1

你可以尝试这样的事情: -

public boolean isNullOrEmpty(String str) { 
    return (str == null) || (str.trim().length() == 0); 
} 

这将返回true如果您Stringnullempty,否则false

0
boolean validateNullString(String str) 
    { 
     return (str == null || str.trim().length() == 0); 

    } 

这将验证您的字符串对象为null和空。

1

根据Apache Commons,可以使用theire方法检查字符串是否为空。

/** 
* <p>Checks if a String is whitespace, empty ("") or null.</p> 
* 
* <pre> 
* StringUtils.isBlank(null)  = true 
* StringUtils.isBlank("")  = true 
* StringUtils.isBlank(" ")  = true 
* StringUtils.isBlank("bob")  = false 
* StringUtils.isBlank(" bob ") = false 
* </pre> 
* 
* @param str the String to check, may be null 
* @return <code>true</code> if the String is null, empty or whitespace 
* @since 2.0 
*/ 
public static boolean isBlank(String str) { 
    int strLen; 
    if (str == null || (strLen = str.length()) == 0) { 
    return true; 
    } 
    for (int i = 0; i < strLen; i++) { 
    if ((Character.isWhitespace(str.charAt(i)) == false)) { 
     return false; 
    } 
    } 
    return true; 
} 

之所以这样是体现在这个例子中:

System.out.println(Character.isWhitespace('c')); // false 
System.out.println(Character.isWhitespace(' ')); // true 
System.out.println(Character.isWhitespace('\n')); // true 
System.out.println(Character.isWhitespace('\t')); // true 
0

试试这个:

private boolean validaterow(String g){ 
boolean isValid = false; 
if(g.trim().isEmpty()){ 
    isValid = true; 
} 
return isValid; 

}

如果参数字符串不是空的方法返回false。从apache.commons.lang

If (StringUtils.isNotBlank(yourString)){ 

     isValid = true; 
} 

0

使用StringUtils类这将修剪yourString和检查空或空值。