[Java] [37] string taken

text:

String str = "123abc456";

1, taking the first n characters of the string

str = str.Substring(0, n);  // or  str = str.Remove(n, str.Length - n); 

2, to remove the first n characters of the string

str = str.Remove(0, n);  // or str = str.Substring(n); 

3, taken from the right n characters

str = str.Substring(str.Length - n);  // or str = str.Remove(0, str.Length - n);

4, from the right to remove n characters

str = str.Substring(0, str.Length - n);  // or str = str.Remove(str.Length- n , n);

5, if there is "abc" is replaced with the string "ABC"

 str = str.Replace("abc","ABC");

6, if there is "abc" is determined to remove the string has

   Regex r = new  Regex("abc"); 
   Match m = r.Match(str); 
   if(m.Success) {
      str =nstr.Replace("abc" ,"");
   }

Reference blog:

Interception of a string of several frequently used - Lu Qingsong - blog Park
https://www.cnblogs.com/luqingsong/archive/2011/02/16/1956458.html

Guess you like

Origin www.cnblogs.com/huashengweilong/p/11273585.html