一、string分割字符串
string也可以稱為字符串,在編程中經常需要對字符串進行分割。string的split()方法可以將字符串按指定的分隔符分割成多個字符串,生成一個字符串數組。
string str = "hello,world"; string[] words = str.Split(','); foreach(string word in words) { Console.WriteLine(word); }
上述代碼將字符串”hello,world”使用逗號分割成兩個字符串”hello”和”world”。
在split()方法中,如果沒有指定分隔符,則默認使用空格作為分隔符。例如,下面代碼將字符串”hello world”使用空格分割成兩個字符串。
string str = "hello world"; string[] words = str.Split(); foreach(string word in words) { Console.WriteLine(word); }
二、string切割碰到符號相同
在string切割過程中,碰到符號相同的情況也是非常常見的。而本身的split()方法只能支持單一的分隔符,那麼碰到符號相同的情況下,我們該怎麼處理呢?
這時可以使用string.IndexOf()方法和string.Substring()方法來實現自定義分割。
string str = "hello|world|welcome"; int index1 = str.IndexOf('|'); int index2 = str.IndexOf('|', index1 + 1); string subStr1 = str.Substring(0, index1); string subStr2 = str.Substring(index1 + 1, index2 - index1 - 1); string subStr3 = str.Substring(index2 + 1); Console.WriteLine(subStr1); Console.WriteLine(subStr2); Console.WriteLine(subStr3);
上述代碼將字符串”hello|world|welcome”分隔成了三個字符串:”hello”、”world”和”welcome”,其中用到了IndexOf()方法和Substring()方法。
三、string切割方法
除了使用split()方法和自定義分割的方法外,還有一些string切割的方法。
- Remove():可以從字符串中刪除指定數量的字符。
- Replace():可以將字符串中的一個字符替換成另一個字符。
- Insert():可以在字符串的指定位置插入另一個字符串。
- Substring():可以獲取字符串的子字符串。
- Trim():可以刪除字符串兩端的空格。
下面是使用SubString()方法實現字符串分割的示例。
string str = "hello,world"; string subStr1 = str.Substring(0, 5); string subStr2 = str.Substring(6); Console.WriteLine(subStr1); Console.WriteLine(subStr2);
上述代碼將字符串”hello,world”分隔成了兩個字符串:”hello”和”world”。
四、string切割最後一個
如果需要將一個字符串按照某個分隔符進行切割,但是最後一個分隔符後面的部分並沒有內容,這時可以使用string.LastIndexOf()方法。
string str = "hello,world,"; int index = str.LastIndexOf(','); string subStr1 = str.Substring(0, index); string subStr2 = str.Substring(index + 1); Console.WriteLine(subStr1); Console.WriteLine(subStr2);
上述代碼將字符串”hello,world,”分隔成了兩個字符串:”hello”和”world”,並且忽略了最後一個逗號。
五、string切割字符串成數組
除了使用split()方法外,還可以使用Regex.Split()方法將字符串切割成數組。
string str = "hello world"; string[] words = Regex.Split(str, " "); foreach(string word in words) { Console.WriteLine(word); }
上述代碼使用空格作為分隔符將字符串”hello world”切割成了兩個字符串:”hello”和”world”。
六、string字符串截取
string字符串的截取操作是非常常見的,通過設置起點和終點實現截取操作。
string str = "hello,world"; string subStr1 = str.Substring(0, 5); string subStr2 = str.Substring(5, 5); Console.WriteLine(subStr1); Console.WriteLine(subStr2);
上述代碼將字符串”hello,world”分隔成了兩個字符串:”hello”和”,world”。
七、string字符串分割
如果需要將字符串按照某個分隔符分割成多個字符串,則可以使用split()方法。
string str = "hello,world,welcome"; string[] words = str.Split(','); foreach(string word in words) { Console.WriteLine(word); }
上述代碼將字符串”hello,world,welcome”使用逗號分割成了三個字符串:”hello”、”world”和”welcome”。
八、string拆分字符串的方法
字符串的拆分是將一個字符串按照某個規則拆分成多個字符串的操作。拆分常常用於從字符串中提取信息。
下面是使用string.IndexOf()方法和string.Substring()方法實現字符串拆分的示例。
string str = "hello|world|welcome"; int index1 = str.IndexOf('|'); int index2 = str.IndexOf('|', index1 + 1); string subStr1 = str.Substring(0, index1); string subStr2 = str.Substring(index1 + 1, index2 - index1 - 1); string subStr3 = str.Substring(index2 + 1); Console.WriteLine(subStr1); Console.WriteLine(subStr2); Console.WriteLine(subStr3);
上述代碼將字符串”hello|world|welcome”拆分成了三個字符串:”hello”、”world”和”welcome”。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/246466.html