2017-02-11 22 views
1

我在登錄頁面中有一個編輯文本,並且我有編輯文本MaxLength =「12」的條件。在我的JSON響應中,我有一個包含15個字母(或者是Nor)的字符串,我需要檢查(或者比較)最多12個字母(或者是否),如果兩個字母相同,最多12個字母(或者是Nor),那麼我必須進入我的應用程序。JSON響應與限制數據的比較

{ 
ID No="123456789012.00" 
} 
+0

您好@suresh你是否從任何答案獲得幫助? – MilapTank

回答

1

你可以用 「子」 和 「等於」 方法: (我假設你的字符串的名稱editTextString和jsonString)

首先,從JSON字符串會首先12個字符的字符串:

String jsonFirst12Chars = jsonString.substring(0,12); 

然後使用equals方法對它們進行比較:

if(editTextString.equals(jsonFirst12Chars)) { 
// They are same, do something 
} else { 
// They are not same.. 
} 
2

您好Suresh使用String#substring

try { 
     JSONObject jObj = new JSONObject("{\"id_no\":\"123456789012.00\"}"); 
     String strJNumber = jObj.optString("id_no").substring(0,11); 
     String strENumber = etNumber.getText().toString(); 

     if(strENumber.equals(strJNumber)){ 
      // TODO: true both are same 
     }else { 
      // TODO: true both are not same 
     } 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    }