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

How to convert string in text box to an array?
I mean if "000011" is in text box it converts to:
a[0]=0
a[1]=0
a[2]=0
a[3]=0
a[4]=1
a[5]=1
Please help me.

Thanks in Advance.
Posted
Updated 12-Jun-11 23:16pm
v3

Try my Solution

char[] strArr = textBox1.Text.ToCharArray();
 
Share this answer
 
v3
There are two ways: Split[^] and ToCharArray[^]
The former returns an array of strings, which were separated by a character - CSV for example:
string myString = "123,456,789";
string[] parts = myString.Split(',');
Would return three strings, "123", "456", and "789".
The latter would give you an array of each character:
string myString = "123,456,789";
char[] chars = myString.ToCharArray();

Alternatively, you can use the string as an array of chars directly:
string myString = "123,456,789";
char c = myString[2];
would give you the character '3'
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Jun-11 3:55am    
Simpler, simpler! It's just conversion to an array...
Please see my solution.
--SA
Try that

TextBox1.Text.ToCharArray()
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Jun-11 3:58am    
Correct, a 5.
Please see one more variant in my solution.
--SA
Tarun.K.S 13-Jun-11 4:07am    
Good answer. 5+
BTW is your real name Nitin? :)
nit_singh 13-Jun-11 5:01am    
Yes Tarun. this is my real name. and Thanks for your 5
nit_singh 13-Jun-11 5:20am    
but how do you know my real name?????
Tarun.K.S 13-Jun-11 6:22am    
Lol just a wild guess! ;)
you wrote nit, so I thought it might be Nitin. :D
Hey do join us here : General Indian Topics. A forum for us Indians. :)
C#
char[] myArray = myTextBox.Text.ToArray<char>();

//same thing, would work with C# 2.0:
myArray = myTextBox.Text.ToCharArray();


Why doing so?!

—SA
 
Share this answer
 
v3
Comments
Tarun.K.S 13-Jun-11 4:07am    
Good answer SA. 5+
Sergey Alexandrovich Kryukov 13-Jun-11 5:28am    
Thank you, Tarun.
--SA
You can do this way:

C#
string myString = textbox1.text;
string[] myArray= new string[myString.Length];
for(int i = 0; i<myString.Length; i++){
myArray[i] = myString[i].ToString();
}



hope this helps :)
 
Share this answer
 
v3
string mys = textBox1.Text;
string[] myarr = new string[mys.Length];
for (int z = 0; z < mys.Length; z++)
{
myarr[z] = mys[z].ToString();
}
int count=0;

for (int i = 0; i <= 3; i++)
{
string c = myarr[i].ToUpper();
if (myarr[i] == c)
{
count++;
}
}
MessageBox.Show("Count=" + count + "");

Hope this helps...
 
Share this answer
 
Comments
CHill60 25-Jan-14 12:33pm    
Reasons for my downvote... the question is over 2 years old; there is no relation to the OPs question with the "count"; the bit that would have answered the OPs question had already been given in solutions 2 and 3 and 4 and 5 and 6

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