简体   繁体   English

C#从字符串中删除第n个项目

[英]C# Delete the nth item from a string

How would i go about deleting (not just replacing with a ' ' ) The nth char of a string. 我将如何删除(而不仅仅是用' '代替)字符串的第n个字符。 Say i want to have Hello World output Hllo World any thing that could do this? 说我想让Hello World输出Hllo World可以做到的任何事情?

With .Remove 用。删除

var removed = s.Remove(1, 1);

note, you can't change a string, you can only create a new string with the character removed. 请注意,您不能更改字符串,只能创建一个删除了字符的新字符串。

字符串类具有remove方法

var s = "abc".Remove(1,1); //will return ac 
StringBuilder a = new StringBuilder("Hello World");
a.remove(1, 1);
string stringFlag = "ImAFlag";

var charList = stringFlag .Select(c => c.ToString()).ToArray();

charList.RemoveAt(0);

string newString = null;
foreach(var item in charList)
{
    newString += item.ToString();
}

now the stringFlag is 现在stringFlag是

"mAFlag"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM