Click here to Skip to main content
15,886,840 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
how to read stringbuilder values one by one like string
stringbuilder sb []
for(int i =0; i< sb.length; i++)
{
messagebox.show(sb[i])
}
Posted
Updated 5-Jul-11 1:27am
v2
Comments
Tarun.K.S 5-Jul-11 7:34am    
Do you have an array of StringBuilder?
kami124 5-Jul-11 7:40am    
no i dont have me getting the values from sql server and saving in stringbuilder

You can read character by character.
C#
StringBuilder sb;
foreach (char k in sb.ToString())
{
}
 
Share this answer
 
Comments
StM0n 5-Jul-11 7:30am    
He's using a array... never mind the fact he didn't inizialize it :)
Abhinav S 5-Jul-11 8:07am    
I did not initialize my Stringbuilder
Just demo code. :)
StM0n 5-Jul-11 8:09am    
No problem :)
Your question is unclear to me.

If you have an array of StringBuilder, then do this:

C#
StringBuilder[] sb = new StringBuilder[] { new StringBuilder("A"), new StringBuilder("B") };
foreach (StringBuilder s in sb)
{
    MessageBox.Show(s.ToString());
}


If that is not the case. Then do this:

C#
StringBuilder sb = new StringBuilder("Tarun");
for (int i = 0; i < sb.Length; i++)
{
   MessageBox.Show(sb[i].ToString());
}
 
Share this answer
 
Comments
StM0n 5-Jul-11 8:11am    
[no_offense]Sorry, but didn't I already said this[/no_offense]
Tarun.K.S 5-Jul-11 8:25am    
Firstly, the answer of yours that I saw after I posted my answer was this: "Use sb[i].ToString()... THE swiss-army-knife of .Net-Classes." So I didn't copy or plagiarize your answer. You can see that I haven't edited my answer since I posted. You have revised your answer 5 times, so it seems quite natural that you might have looked at my answer and then edited which may not be the case.

Secondly, while I was writing my solution, I couldn't see any other answers. I was preparing my solution.

Thirdly, I didn't downvote your answer.
StM0n 5-Jul-11 8:30am    
1. No problem... never thought about plagiarization, rather like I said something wrong/uncorrect.
2. Me too...? Strange???
3. Never assumed it... but I upvoted yours due the fact it's more precise :thumbsup:
Tarun.K.S 5-Jul-11 9:07am    
I'm glad that the confusion is over! Let me fix that down-vote. Cheers!
Use sb[i].ToString()... THE swiss-army-knife of .Net-Classes

C#
StringBuilder[] sb = new StringBuilder[10];
for (int index = 0; index < sb.Length; index++) {
    sb.GetValue(index).ToString();
}


if you wantto read all characters in a StringBuilder like an array, this one would be more appropriate

C#
StringBuilder sb = new StringBuilder();
for (int index = 0; index < sb.Length; index++) {
    sb[index].ToString();
}
 
Share this answer
 
v5
Comments
kami124 5-Jul-11 7:35am    
how do i come to know that what is the length of stringbuilder

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