简体   繁体   English

在字符串开头添加/删除字符的最有效方法?

[英]Most efficient way of adding/removing a character to beginning of string?

I was doing a small 'scalable' C# MVC project, with quite a bit of read/write to a database. 我当时正在做一个小型的“可扩展” C#MVC项目,对数据库进行了大量读/写操作。

From this, I would need to add/remove the first letter of the input string. 由此,我需要添加/删除输入字符串的第一个字母。


'Removing' the first character is quite easy (using a Substring method) - using something like: “删除”第一个字符非常容易(使用Substring方法)-使用类似以下内容:

String test = "HHello world";
test = test.Substring(1,test.Length-1);

'Adding' a character efficiently seems to be messy/awkward: 有效地“添加”一个角色似乎很麻烦/尴尬:

String test = "ello World";
test = "H" + test;

Seeing as this will be done for a lot of records, would this be be the most efficient way of doing these operations? 鉴于将对许多记录执行此操作,这将是执行这些操作的最有效方法吗?

I am also testing if a string starts with the letter 'T' by using, and adding 'T' if it doesn't by: 我还通过使用来测试字符串是否以字母“ T”开头,如果不是,则添加“ T”:

String test = "Hello World";
if(test[0]!='T')
{
    test = "T" + test;
}

and would like to know if this would be suitable for this 并想知道这是否适合

Both are equally efficient I think since both require a new string to be initialized, since string is immutable. 我认为两者都是同等有效的,因为string都是不可变的,因此都需要初始化新的string

When doing this on the same string multiple times, a StringBuilder might come in handy when adding. 当对同一字符串多次执行此操作时,添加时, StringBuilder可能会派上用场。 That will increase performance over adding. 与添加相比,这将提高性能。

You could also opt to move this operation to the database side if possible. 如果可能,您还可以选择将此操作移至数据库端。 That might increase performance too. 这也可能会提高性能。

If you have several records and to each of the several records field you need to append a character at the beginning, you can use String.Insert with an index of 0 http://msdn.microsoft.com/it-it/library/system.string.insert(v=vs.110).aspx 如果您有多个记录,并且需要在每个记录字段的开头添加一个字符,则可以使用String.Insert ,索引为0 http://msdn.microsoft.com/it-it/library/ system.string.insert(v = vs.110)的.aspx

string yourString = yourString.Insert( 0, "C" );

This will pretty much do the same of what you wrote in your original post, but since it seems you prefer to use a Method and not an operator... 这几乎与您在原始帖子中写的内容一样,但是由于您似乎更喜欢使用Method而不是运算符...

If you have to append a character several times, to a single string, then you're better using a StringBuilder http://msdn.microsoft.com/it-it/library/system.text.stringbuilder(v=vs.110).aspx 如果必须多次向单个字符串附加一个字符,则最好使用StringBuilder http://msdn.microsoft.com/it-it/library/system.text.stringbuilder(v=vs.110 )的.aspx

For removing I would use the remove command as this doesn't require to know the length of the string: 对于删除,我将使用remove命令,因为这不需要知道字符串的长度:

test = test.Remove(0, 1);

You could also treat the string as an array for the Add and use 您也可以将字符串视为添加和使用的数组

test = test.Insert(0, "H");

If you are always removing and then adding a character you can treat the string as an array again and just replace the character. 如果您总是要删除然后再添加一个字符,则可以将字符串再次视为数组,而只需替换字符即可。

test = (test.ToCharArray()[0] = 'H').ToString();

When doing lots of operations to the same string I would use a StringBuilder though, more expensive to create but faster operations on the string. 当对同一字符串进行大量操作时,我会使用StringBuilder虽然创建起来比较昂贵,但是对字符串的操作更快。

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

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