2012-09-13 58 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