2012-09-13 37 views
7

我在Android代碼中使用了StringUtils類。但生成異常,即找不到類。但是,我已經使用了該類的jar文件。請幫幫我!StringUtils類不能在Android中工作

+1

你有沒有添加jar文件到構建路徑? –

+0

@Chirag Raval:是的,我已經使用了jar文件。 –

+0

這在簡單的java程序中工作正常。但是當我們在android中使用相同的代碼時,會產生這種類型的錯誤。請任何人有想法,所以請幫助我。 –

回答

0

我的問題用下面的代碼解決,我們不需要使用StringUtils jar來添加我們的項目。

我創建自己的類,即「RanjitString」替換爲StringUtils。

public class RanjitString { 
public static String substringBetween(String str, String open, String close) { 
    if (str == null || open == null || close == null) { 
     return null; 
    } 
    int start = str.indexOf(open); 
    if (start != -1) { 
     int end = str.indexOf(close, start + open.length()); 
     if (end != -1) { 
      return str.substring(start + open.length(), end); 
     } 
    } 
    return null; 
} 
} 

並直接訪問靜態方法substringBetween:

String myxml="This is my xml with different tags"; 
String successResult = RanjitString.substringBetween(myxml, 
       "<tag>", "</tag>"); 

此代碼爲我工作...

+0

請參閱@ user2160667的其他答案,使用TextUtils –

+1

@SerkanArıkuşu:但是沒有substringBetween方法textUtils –

24

儘量不要使用外部庫。請始終尋找Android替代品。

對於這個特定的情況下,你可以使用:android.text.TextUtils

+0

這應該被標記爲正確的答案。 – Desmond

相關問題