2013-05-03 94 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和檢查空或空值。