C# 在字符串指定位置之前插入新的字符串

2025-02-24 08:47:59
推荐回答(4个)
回答1:

static void Main(string[] args)

{

  string str = "qewdesdfdfdfd.txt";

  string insertStr = "123";

  // 找到最后一个【.】点的位置,然后插入你想插入的字符

  string result = str.Insert(str.LastIndexOf('.'), insertStr);

  Console.WriteLine(result);

  Console.ReadLine();

}

运行结果:

回答2:

这个txt是固定的最后一个txt吗?
比如 txtqewdesdfdfdfd.txt 这个字符串,获取 最后的那个txt的索引就行,然后在这个索引的位置插入新字符

回答3:

IndexOf(".text")利用这个定位字符串.tet~~~~然后再插入就好了~~~
String str = "qewdesdfdfdfd.txt"

str1 = "123"

aaa.Insert(aaa.IndexOf(".txt"), str1);

回答4:

试试这样:

String aaa = "qewdesdfdfdfd.txt"

aaa.Insert(aaa.IndexOf(".txt"), "123");