返回顶部
首页 > 资讯 > 移动开发 >Android 字符串的替换,截取,拆分,拼接
  • 249
分享到

Android 字符串的替换,截取,拆分,拼接

android 2023-09-03 18:09:25 249人浏览 八月长安
摘要

1 去除字符串中的 [ ] , " ", 逗号替换成 & 符号 public static String ReplaceString(List info) { String infoJSON = jsONObject.t

1 去除字符串中的 [ ] , " ", 逗号替换成 & 符号

   public static String ReplaceString(List info) {        String infoJSON = jsONObject.toJSONString(info);        String replace = infoJson.replace("]", "").replace("[", "").replace(",", "&").replace("\"", "");        return replace;    }

2 字符拆分取任意部分

    public static String SplitString(String info, int num) {        String[] temp = null;        temp = info.split(",");        return temp[num];    }

3 取字符串前 i 个字符

    public static String InterceptString(String info, int i) {        String substring = info.substring(0, i);        return substring;    }

4 取字符索引 i 之后的字符

    public static String DeleString(String info, int i) {        String substring = info.substring(i);        return substring;    }

5 从右边开始取 i 个字符

    public static String RightString(String info, int i) {        String substring = info.substring(info.length() - i);        return substring;    }

6 从右边开始去掉 i 个字符

   public static String RightdeleString(String info, int i) {        String substring = info.substring(0, info.length() - i);        return substring;    }

7 字符取索引 i 到 索引 n 之间的字符 (不包含 n )

    public static String RepinterString(String info, int i, int n) {        String substring = info.substring(i, n);        return substring;    }

8 获取指定字符之后的字符

    public static String AfterString(String info, String appoint) {        String substring = info.substring(info.indexOf(appoint));        return substring;    }

9 获取从第 i 位开始,截取第一次遇到 “.” 的字符串

    public static String AftenumString(String info, int i) {        String substring = info.substring(i, info.indexOf(".", i));        return substring;    }

10 字符串自定义拼接

    public static String AppendString(Item info) {        StringBuffer strBuffer = new StringBuffer();        strBuffer.append("仓库: " + info.getStoreShelf().getWarehouse() + "\n");        strBuffer.append("货架: " + info.getStoreShelf().getShelf() + "\n");        strBuffer.append("架层: " + info.getStoreShelf().getLayer() + "\n");        strBuffer.append("分区: " + info.getStoreShelf().getSection() + "\n");        return strBuffer.toString();    }

最后贴上完整工具类,以便使用

public class ReplaceUtils {    //字符替换    public static String ReplaceString(List info) {        String infoJson = JSONObject.toJSONString(info);        String replace = infoJson.replace("]", "").replace("[", "").replace(",", "&").replace("\"", "");        return replace;    }    //字符拆分取任意部分    public static String SplitString(String info, int num) {        String[] temp = null;        temp = info.split(",");        return temp[num];    }    //取字符串前 i 个字符    public static String InterceptString(String info, int i) {        String substring = info.substring(0, i);        return substring;    }    //取字符索引 i 之后的字符    public static String DeleString(String info, int i) {        String substring = info.substring(i);        return substring;    }    //从右边开始取 i 个字符    public static String RightString(String info, int i) {        String substring = info.substring(info.length() - i);        return substring;    }    //从右边开始去掉 i 个字符    public static String RightdeleString(String info, int i) {        String substring = info.substring(0, info.length() - i);        return substring;    }    //字符取索引 i 到 索引 n 之间的字符 (不包含 n )    public static String RepinterString(String info, int i, int n) {        String substring = info.substring(i, n);        return substring;    }    //获取指定字符之后的字符    public static String AfterString(String info, String appoint) {        String substring = info.substring(info.indexOf(appoint));        return substring;    }    //获取从第 i 位开始,截取第一次遇到 “.” 的字符串    public static String AftenumString(String info, int i) {        String substring = info.substring(i, info.indexOf(".", i));        return substring;    }    //字符串自定义拼接    public static String AppendString(Item info) {        StringBuffer strBuffer = new StringBuffer();        strBuffer.append("仓库: " + info.getStoreShelf().getWarehouse() + "\n");        strBuffer.append("货架: " + info.getStoreShelf().getShelf() + "\n");        strBuffer.append("架层: " + info.getStoreShelf().getLayer() + "\n");        strBuffer.append("分区: " + info.getStoreShelf().getSection() + "\n");        return strBuffer.toString();    }}

来源地址:https://blog.csdn.net/X_sunmmer/article/details/130390346

--结束END--

本文标题: Android 字符串的替换,截取,拆分,拼接

本文链接: https://lsjlt.com/news/392609.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作