abcghj
123|456|789
四、字符串的替换
字符串的替换是通过Replace方法实现的,格式为:字符串.Replace(要替换的原字符串,替换后的字符串);
例四,实现对字符串str的替换
?
- <span style="font-size:18px;">using System; using System.Collections.Generic;
- using System.Linq; using System.Text;
- using System.Threading.Tasks;
- namespace 字符串 {
- class Program {
- static void Main(string[] args) {
- string str = "abcadfaslfj"; string replacestr = str.Replace("a","|");//用“|”替换“a”
- Console.WriteLine(replacestr); Console.ReadLine();
- } }
- }</span>
输出的结果为:|bc|df|slfj
五、字符串的插入与填充
字符串的插入是通过Insert方法实现的,其格式为:字符串.Insert(插入位置,插入字串)
字符串的填充是通过PadRight方法和PadLeft方法实现的。
PadRight方法是在字符串的结尾通过添加指定的重复字符填充字符串,格式为:字符串.PadRight(总长度)(以空格填充)和字符串.PadRight(总长度,要填充的字符)。
PadLeft方法是在字符串的开头通过添加指定的重复字符填充字符串,格式为:字符串.PadLeft(总长度)(以空格填充)和字符串.PadLeft(总长度,要填充的字符)。
例五、实现对字符串str的插入与填充
- <span style="font-size:18px;">using System; using System.Collections.Generic;
- using System.Linq; using System.Text;










