EDITORIAL: A few days ago Getting a little python, found inside the string interception is useful, it is specially to collect a bit of C # string interception, tidy up for a rainy day.
C # string split
- Dividing a single delimiter string, where the output is the result
. 1
2
. 3
. 4
. 5
when a ',' number after split, will be removed, '' symbol, and the type noted in the Split instead of char string, so use single quotes instead of double quotes.
static void Main(string[] args)
{
string a = "1,2,3,4,5";
string[] b = a.Split(',');
foreach(string c in b)
Console.WriteLine(c);
}
- Division string using a plurality of separators, where the result is output
. 1
2
. 3
4 + 5
when using a plurality of separators, the Split is a char array, all values are in line with the array is considered as a delimiter.
static void Main(string[] args)
{
string a = "1,2,3-4+5";
string[] b = a.Split(new char[2] { ',', '-' });
foreach (string c in b)
Console.WriteLine(c);
}
- A single or multiple divided string string, only when the value of a string array, i.e. a single string dividing, when a plurality of values within the array, i.e. multi-division string, where the result is output
20
8
9
924
static void Main(string[] args)
{
string a = "201819192425";
string[] b = a.Split(new string[] { "1","25" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string c in b)
Console.WriteLine(c);
}
C # string interception
- Taken before the i-th character string, the front display or remove the i-th and i-th string after the string to obtain a new principle, b and c values are herein
20181
20181
static void Main(string[] args)
{
int i = 5;
string a = "201819192425";
string b = a.Substring(0, i);
//显示从第0个到第i个字符,但不包括第i个
string c = a.Remove(i, a.Length - i);
//返回一个从i个开始删除,删除长度为总长度减i个(即删除i及之后的字符串)
Console.WriteLine("{0}\n{1}",b,c);
}
- I before deleting a string string, and the last principle is just the opposite, where the values of b and c are
9192425
9192425
static void Main(string[] args)
{
int i = 5;
string a = "201819192425";
string b = a.Remove(0, i);
//删除第0个到第i个字符,但不包括第i个
string c = a.Substring(i);
//从第i个字符开始,显示到结尾
Console.WriteLine("{0}\n{1}",b,c);
}
- I-th character, principle and <1>, taken after a similar string, the value b and c are herein
92425
92425
static void Main(string[] args)
{
int i = 5;
string a = "201819192425";
string b = a.Substring(a.Length - i);
//从第(总长度-i)个字符开始(即从倒数第i个开始),显示到结尾
string c = a.Remove(0,a.Length-i);
//删除第0个字符到第(总长度-i)个字符,返回余下的字符串
Console.WriteLine("{0}\n{1}",b,c);
}
- After deleting the i-th string string, the previous principle coincides with the contrast, the value of b and c are herein
2018191
2018191
static void Main(string[] args)
{
int i = 5;
string a = "201819192425";
string b = a.Substring(0, a.Length - i);
//从第0个字符开始,显示到(总长度-i)个字符,但不包括该字符
string c = a.Remove(a.Length - i, i);
//删除从第(总长度-i)个字符开始,长度为i的字符串,包括第(总长度-i)个字符
Console.WriteLine("{0}\n{1}", b, c);
}
- Interception of the string starting from the i-th, j is the length of the string, the comment indicated principle, where b is the value
18191
static void Main(string[] args)
{
int i = 3, j = 5;
string a = "201819192425";
string b = a.Substring(i - 1, j);
//因为排序从0开始,所以(i-1),即从(i-1)个字符开始显示长度为j的字符串
//就是从客观上从第三个开始显示到第(3+5-1)个字符,因为第i个也占一个长度
Console.WriteLine(b);
}
- Remove the string starting from the i-th, j is the length of the string, the opposite principle, only change the code assignment to the string b, where b is the value
2,092,425
static void Main(string[] args)
{
int i = 3, j = 5;
string a = "201819192425";
string b = a.Remove(i - 1, j);
//删除第i个字符到第(i+j-1)个字符,并返回新字符串
Console.WriteLine(b);
}
- Alternatively string specified string, the string will meet the requirements of all the fragments to be replaced with another string fragment has been given, where the value of b
20186666662425
static void Main(string[] args)
{
string a = "201819192425";
string b = a.Replace("19", "666");
//每当旧串中出现'19'时,即替换为'666',并将返回的新串赋值于b
//因为出现两次'19',所以也替换了两次'666'
//两者长度不一样所以替换后长度也发生改变
Console.WriteLine(b);
}
- Removes the specified string in a string, the string will meet the requirements of all the segments are replaced by a null value, to achieve the effect of deletion, where b is the value
20182425
static void Main(string[] args)
{
string a = "201819192425";
string b = a.Replace("19", "");
//每当旧串中出现'19'时,替换为一个空串,可视为删除
Console.WriteLine(b);
}
- Finally a string value taken to meet the requirements of this press '9' of the string is divided, so the value of b
2425
static void Main(string[] args)
{
string a = "201819192425";
string b = a.Substring(a.LastIndexOf("9") + 1);
//显示按要求分割的最后一个值
Console.WriteLine(b);
}
Epilogue
Because of shallow knowledge, if insufficient correction also hope to work together, refueling, thank you, thank you