简体   繁体   English

如何在每个字符处分割字符串

[英]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. 我正在使用ASP.NET网页创建一个项目,在其中分配了用户(客户)名的每个字符到每个单独的输入块中。

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 . 为此,我从Sql Server CE数据库获取值,然后尝试将其转换为每个输入一个字符组成的数组。 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) http://msdn.microsoft.com/zh-cn/library/8eb78ww7(v = vs.90) .aspx (编译器错误: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 . 名称为: 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() . 您可以尝试使用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[]

char[] characters = yourstring.ToCharArray();

Try name_emp.ToArray() . 尝试name_emp.ToArray() A string implements IEnumerable<char> , so ToArray will return an array of the char . 字符串实现IEnumerable<char> ,因此ToArray将返回char的数组。

Edit: Actually I suppose ToCharArray is better in this case... 编辑:实际上我想在这种情况下ToCharArray更好...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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