0

Hi i have the following code:

static void CalcWordchange()
{
    List<string[]> l = new List<string[]>
        {
        new string[]{Question1, matcheditalian1},
        new string[]{"Sam", matcheditalian2},
        new string[]{"clozapine", matcheditalian3},
        new string[]{"flomax", matcheditalian4},
        new string[]{"toradol", matcheditalian5},
        };

    foreach (string[] a in l)
    {
        int cost = LevenshteinDistance.Compute(a[0], a[1]);
        errorString = String.Format("To change your input: \n {0} \n into the correct word: \n {1} \n you need to make: \n {2} changes \n ".Replace("\n",      Environment.NewLine),
            a[0],
            a[1],
            cost);
    }
}

Each time a button is clicked the text in the foreach loop runs and outputs a single sentence (the last item in the list). What i want to happen is to output all 5 items into a string.

I have added 4 new variables (errorString2, 3 etc) but cannot work out how to output it.

Any help is appreciated, Thanks

3 Answers 3

5

Try using a StringBuilder object to collect all the parts.

StringBuilder buildString = new StringBuilder();
foreach (string[] a in l)
{
    int cost = LevenshteinDistance.Compute(a[0], a[1]);
    buildString.AppendFormat("To change your input: \n {0} \n into the correct word: \n {1} \n you need to make: \n {2} changes \n ".Replace("\n",      Environment.NewLine),
        a[0],
        a[1],
        cost);
}
errorString = buildString.ToString();
Sign up to request clarification or add additional context in comments.

3 Comments

How does buildString.AppendLine differ from string += ? I'm assuming it is a better optimization in speed vs. the concatenation you referenced in my post?
@wjhguitarman: Yes. The string concatenation creates and throws away a bunch of temporary strings. StringBuilder preallocates a buffer and writes several items into it, then extends the buffer if it runs out. So both faster and uses less memory.
@wjhguitarman: String Concatenation creates a new string each time it is used (i.e. each pass through the loop), where as StringBuilder uses a self-growing char[] behind the scenes (until you call .ToString()). If that runs out of room, then StringBuilder will create a new array that is double the size of the old array. This means that it will take longer and longer for the array to need to be re-created, rather than one new string object every pass of the loop. String Concatination is still good for non-loops, especially if you are just adding 2-4 strings together.
2

Instead do something like this:

 string finalOuput = string.empty;
 foreach (string[] a in l)
 {
  int cost = levelshteinDstance.Compute(a[0], a[1]);
  finalOutput += string.Format("To change your input: \n {0} \n into the correct word: \n {1} \n you need to make: \n {2} changes \n ".Replace("\n",      Environment.NewLine),
            a[0],
            a[1],
            cost);
    }
}

//display finalOutput here

1 Comment

Please don't recommend string concatentation in a loop.
1

Create a List<string> to hold the output:

var OutputList = new List<string>();
foreach (string[] a in l)
{
    errorString = ...
    OutputList.Add(errorString);
}

// output
foreach (var s in OutputList)
{
    Console.WriteLine(s);
}

Or you could use a StringBuilder:

var outputS = new StringBuilder();
foreach (string[] a in l)
{
    errorstring = ...
    outputS.AppendLine(errorString);
}

Console.WriteLine(outputS.ToString());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.