简体   繁体   中英

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. Say i want to have Hello World output Hllo World any thing that could do this?

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

"mAFlag"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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