469,294 Members | 1,742 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,294 developers. It's quick & easy.

C# - split method - cannot convert char to string

147 100+
Hi

I've had a few error messages with this I don't understand why. It works fine in a Windows Form, but now moving it to a website, it's not working. I don't have the option to count the lines as I did with Windows Forms so I assume the best way is to use the split method. However, the split method only returns Character Arrays, and I want a string, so am calling the split from the Text.RegularExpression namespace.

I am trying to split the lines entered into a multiline textbox line by line, and then peform some logic in each array.

I am told it can't convert char to string.

My code

Expand|Select|Wrap|Line Numbers
  1.             string[] arrS = System.Text.RegularExpressions.Regex.Split(txtS.Text,Environment.NewLine);
  2.             foreach(string st in Convert.ToString(arrS))
  3.             {
  4.                      //code
  5.             } 
  6.  
Any ideas what I've done wrong? Or is the foreach loop only reads strings?
Apr 21 '11 #1
3 6264
Ravi L
12
It should have ideally been as below:

foreach(string st in arrS)
{
//code
}
Apr 21 '11 #2
DaveRook
147 100+
I did that originally, same error. I only added the Convert.ToString due to the cannot convert type char to string error message.

Any other ideas?
Apr 21 '11 #3
Aimee Bailey
197 Expert 100+
Are you sure the error you recieved before adding Convert.ToString was on the line you modified? I can't see how an element of string[] would ever be treated as a char. Anyway, instead of using regex, why don't you just use the split method built into the string class:

Expand|Select|Wrap|Line Numbers
  1. string[] arrS = txtS.Text.Split(new string[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);
  2. foreach(string s in arrS)
  3. {
  4.  
  5. }
  6.  
Apr 26 '11 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Brad Moore | last post: by
6 posts views Thread by Jeff Reed | last post: by
5 posts views Thread by Alan Silver | last post: by
2 posts views Thread by Marcelo Muzilli | last post: by
2 posts views Thread by Christophe | last post: by
3 posts views Thread by priyanka | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.