Android 字符串的替换,截取,拆分,拼接
短信预约 -IT技能 免费直播动态提醒
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
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341