Click here to Skip to main content
15,943,082 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am trying to use a StringBuilder to construct a string in an odd format ie
"00 00 00 00 " can someone who is a bit more versed with the string builder tell me what I am doing wrong. the code I have is below
C#
string Credit_Value_String = "00000000";

         StringBuilder ValueUPDwn = new StringBuilder();


         MessageBox.Show("Value is: " + numericUpDown1.Value.ToString());

         //if (IsNumeric(numericUpDown1.Value))
        // {

            for (int i = 0; i <= Credit_Value_String.Length; i+=2)
             {
                  ValueUPDwn.Append(Credit_Value_String.Substring(i,2));
                  if (i < Credit_Value_String.Length - 2)
                  {
                      ValueUPDwn.Append(" ");
                  }

             }

             Credit_Value_String = ValueUPDwn.ToString();
             MessageBox.Show(Credit_Value_String);
        // }
Hovering over ValueUPDwn in debug shows the string in it space correctly "00 00 00 00" which is what is wanted (I'm going to take the value off a numeric up down tool later) however this does not run the length gets up to max (8) and it tries to go over the edge and gets lost. What I donot understand is I have used this method earlier with no problems...as I said I have not used StringBuilders very much in the past, as the code I am trying here has worked before in the project am I going to regret it when a gremlin crawls out later.
Glenn
Posted

I think your problem was the fact that you are grabbing 1 char past the end of the string. In the function below the for loop has Credit_Value_String.length -1.

If your string is not an even number of chars you will also overrun the string length so added: (Credit_Value_String.Length < i+2) ?


C#
static string AnotherCrazyParsingMethod(string Credit_Value_String)
{
   StringBuilder ValueUPDwn = new StringBuilder();

   for (int i = 0; i < Credit_Value_String.Length; i += 2)
   {
       ValueUPDwn.AppendFormat("{0}{1} ", Credit_Value_String[i],           (Credit_Value_String.Length < i+2) ? '\0' : Credit_Value_String[i + 1]);
   }
   return ValueUPDwn.ToString();
}
 
Share this answer
 
v6
Comments
glennPattonWork3 25-Jan-13 11:38am    
Thanks for that, had problem, went took five minutes to look out window came back and thought oh <<bad word!>> let us hope no one sees, that I have asked this!
BC @ CV 25-Jan-13 11:40am    
No worries. Sometimes it just takes another pair of eyes, ones that haven't been up all night staring at a text editor.
OriginalGriff 25-Jan-13 11:41am    
You are right, but the easier way is to change the comparison to "less than". That way, the compiler can generate better code (becaus eit doesn't need the subtract) and the code is easier to read.
BC @ CV 25-Jan-13 11:45am    
Nice. Thanks for the compiler tip, I didn't know that.
You ran off the end of the string:
C#
for (int i = 0; i <= Credit_Value_String.Length; i+=2)
Should be:
C#
for (int i = 0; i < Credit_Value_String.Length; i+=2)
 
Share this answer
 
Comments
glennPattonWork3 25-Jan-13 11:35am    
Thank you Hangs Head in shame!
OriginalGriff 25-Jan-13 11:39am    
We all do it :laugh:
Trouble is you tend to read what you meant to write...or at least I do!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900