开发者

C#/.NET: Reformatting a very long string

I need to read a string, character by character, and build a new string as the output.

What's the best approach to do this in C#?

Use a StringBuilder? Use some writer/stream?

N开发者_如何学编程ote that there will be no I/O operations--this is strictly an in-memory transformation.


If the size of the string cannot be determined at compile time and it may also be relatively large, you should use a StringBuilder for concatenation as it acts like a mutable string.

var input = SomeLongString;

// may as well initialize the capacity as well
// as the length will be 1 to 1 with the unprocessed input.
var sb = new StringBuilder( input.Length );
foreach( char c in input )
{
    sb.Append( Process( c ) );
}


if it's just one string you can use a collection to hold your characters and then just create the string using the constructor:

IEnumerable<char> myChars = ...;
string result = new string(myChars);

Using Linq and with the help of a method ProcessChar(char c) that transforms each character to its output value this could be just a query transformation (using the string constructor that takes an IEnumerable<char> as input):

string result = new string(sourceString.Select(c => ProcessChar(c)));

This is as efficient as using a StringBuilder (since StringBuilder is used internally in the string class to construct the string from the IEnumerable), but much more readable in my opinion.


Stringbuilder is usually a pretty good bet. I've written lots of javascript in webpages using it.


A StringBuilder is good idea for building your new string, because you can efficiently append new values to it. As for reading the characters from the input string, a StringReader would be a sufficient choice.


void Main()
{
    string myLongString = "lf;kajsd;lfkjal;dfkja;lkdfja;lkdjf;alkjdfa";

    var transformedTString = string.Join(string.Empty, myLongString.ToCharArray().Where(x => x != ';'));

    transformedTString.Dump();
}

If you have more complicated logic you can move your validation to separate predicated method

void Main()
{
    string myLongString = "lf;kajsd;lfkjal;dfkja;lkdfja;lkdjf;alkjdfa";

    var transformedTString = string.Join(string.Empty, myLongString.ToCharArray().Where(MyPredicate));

    transformedTString.Dump();
}

public bool MyPredicate(char c)
{
    return c != ';';
}


What's the difference between read string and output string? I mean why do you have to read char by char?

I use this method for reading string

string str = "some stuff";
string newStr = ToNewString(str);


string ToNewString(string arg)
{
   string r = string.Empty;
   foreach (char c in arg)
      r += DoWork(c);
   return r;
}

char DoWorK(char arg)
{
    // What do you want to do here?
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜