1 去除字符串中的 [ ] , " ", 逗号替换成 & 符号
public static String ReplaceString(List<String> 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<String> 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://www.toymoban.com/news/detail-613528.html
文章来源:https://www.toymoban.com/news/detail-613528.html
到了这里,关于Android 字符串的替换,截取,拆分,拼接的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!