简体   繁体   中英

How to split a string at each character

I am using ASP.NET Web Pages to create a project in which I am assigned to place each character of the user's (customer's) name in each seperate input block.

For that I am getting the value from the Sql Server CE Database and then I am trying to convert it into an array of one character for each input . I have tried the following code for that

var form_data = db.QuerySingle("SELECT * FROM Form_Data WHERE Form_Id =@0", form_id);
var name_emp = form_data.Name_Emig;
if(name_emp != null) {
    name_emp = name_emp.ToString().Split('');
}

It generates the following Compiler error:

http://msdn.microsoft.com/en-us/library/8eb78ww7(v=vs.90).aspx (Compiler error: CS1011)

Which means that the character cannot be initialized by an empty value. I want to convert that value in one character each array.

The name is as: Afzaal Ahmad Zeeshan . So, in each input it would be placed inside the value of it. But the code I am using isn't working. Any suggestion to split the string at each character, so the result would be

{'A', 'F', 'Z', 'A', 'A', 'L' ...}

It doesn't matter whether result is Capital case or lower case. I will convert it to the desired one later.

You can try using ToCharArray() .

So your code would be this:

var form_data = db.QuerySingle("SELECT * FROM Form_Data WHERE Form_Id =@0", form_id);
var name_emp = form_data.Name_Emig;
if(name_emp != null) {
    name_emp = name_emp.ToCharArray();
}

You can use this to iterate with each item :

string yourString = "test";

foreach(var character in yourString)
{
 // do something with each character.
}

Or this to get all characters in a char[]

char[] characters = yourstring.ToCharArray();

Try name_emp.ToArray() . A string implements IEnumerable<char> , so ToArray will return an array of the char .

Edit: Actually I suppose ToCharArray is better in this case...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM