C# 程序替换字符串中给定索引的第 n 个字符
csharpprogrammingserver side programming更新于 2024/9/12 10:20:00
首先,设置一个字符串。
string str1 = "Port"; Console.WriteLine("原始字符串: "+str1);
现在将字符串转换为字符数组。
char[] ch = str1.ToCharArray();
将要替换的字符设置为位置的索引。要设置位于第 3rd 位置的字符。
ch[2] = 'F';
要从字符串中删除第 n 个字符,请尝试以下 C# 代码。这里,我们替换第一个字符。
示例
using System; using System.Collections.Generic; public class Demo { public static void Main(string[] args) { string str1 = "Port"; Console.WriteLine("原始字符串: "+str1); char[] ch = str1.ToCharArray(); ch[0] = 'F'; string str2 = new string (ch); Console.WriteLine("新字符串:"+str2); } }
输出
原始字符串:Port 新字符串:Fort