字符串转换为数组中不使用Split函数 [英] Convert string to array in without using Split function

查看:160
本文介绍了字符串转换为数组中不使用Split函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法将一个字符串(ABCDEF)转化为含有其性格( [字符串数组 b,C,D,E,F] ),而不使用 String.Split 功能?

Is there any way to convert a string ("abcdef") to an array of string containing its character (["a","b","c","d","e","f"]) without using the String.Split function?

推荐答案

所以,你要字符串的数组,其中字符每个:

So you want an array of string, one char each:

string s = "abcdef";
string[] a = s.Select(c => c.ToString()).ToArray();

这工作,因为字符串工具的IEnumerable<焦炭> 。因此,选择(C => c.ToString())项目每个字符字符串字符串重新presenting的字符 ToArray的列举投影并将结果转换为字符串

This works because string implements IEnumerable<char>. So Select(c => c.ToString()) projects each char in the string to a string representing that char and ToArray enumerates the projection and converts the result to an array of string.

如果您正在使用C#的是旧版本:

If you're using an older version of C#:

string s = "abcdef";
string[] a = new string[s.Length];
for(int i = 0; i < s.Length; i++) {
    a[i] = s[i].ToString();
}

这篇关于字符串转换为数组中不使用Split函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆