【问题标题】:StringUtils class not working in AndroidStringUtils 类在 Android 中不起作用
【发布时间】:2012-09-13 10:59:56
【问题描述】:

我在 Android 代码中使用 StringUtils 类。但产生了异常,即找不到类。但是我已经为那个类使用了 jar 文件。请帮帮我!

【问题讨论】:

  • 你添加jar文件到构建路径了吗?
  • @Chirag Raval:是的,我已经为此使用了 jar 文件。
  • 这在简单的 java 程序中运行良好。但是当我们在 android 中使用相同的代码时,会产生这种类型的错误。请任何人对此有想法,所以请帮助我。

标签: android string-utils


【解决方案1】:

尽量不要使用外部库。始终寻找 Android 替代品。

对于这种特定情况,您可以使用:android.text.TextUtils

【讨论】:

    【解决方案2】:

    以下代码解决了我的问题,我们不需要使用 StringUtils jar 来添加我们的项目。

    我创建自己的类,即用 StringUtils 替换“RanjitString”。

    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>");
    

    这段代码对我有用...

    【讨论】:

    • 查看@user2160667 的另一个答案,使用TextUtils
    • @SerkanArıkuşu:但是 textUtils 中没有 substringBetween 方法
    猜你喜欢
    • 2012-06-04
    • 1970-01-01
    • 2018-04-15
    • 1970-01-01
    • 1970-01-01
    • 2016-09-08
    • 2013-01-17
    • 2016-04-12
    • 2018-06-18
    相关资源
    最近更新 更多