【Java】【37】截取字符串
正文:
String str = "123abc456";
1,取字符串的前n个字符
str = str.Substring(0, n); // or str = str.Remove(n, str.Length - n);
2,去掉字符串的前n个字符
str = str.Remove(0, n); // or str = str.Substring(n);
3,从右边开始取n个字符
str = str.Substring(str.Length - n); // or str = str.Remove(0, str.Length - n);
4,从右边开始去掉n个字符
str = str.Substring(0, str.Length - n); // or str = str.Remove(str.Length- n , n);
5,如果字符串中有"abc"则替换成"ABC"
str = str.Replace("abc","ABC");
6,判断字符串中是否有"abc" 有则去掉
Regex r = new Regex("abc"); Match m = r.Match(str); if(m.Success) { str =nstr.Replace("abc" ,""); }
参考博客:
几个经常用到的字符串的截取 - 卢青松 - 博客园
https://www.cnblogs.com/luqingsong/archive/2011/02/16/1956458.html