A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

怎么将一个 string =" abcdefg";的每一个字符赋给一个char[]中呢?

8 个回复

倒序浏览
     是C#的
回复 使用道具 举报
利用字符串截取每个字符,然后将截取的每个字符添加到char[]数组中
回复 使用道具 举报
本帖最后由 赵宗荣 于 2013-7-18 10:42 编辑
  1.             string strs = "badfefefegef";
  2.             char[] ch =new char[strs.Length];
  3.             for (int i = 0; i < strs.Length; i++)
  4.             {
  5.                 ch[i] = strs[i];//将字符串的单个字符赋值给在char数组中相应位置的值
  6.              }
  7.             for (int i = 0; i < ch.Length; i++)
  8.             {
  9.                 Console.Write(ch[i].ToString()+"\r\n");//将每一个char数组中的值打印出来
  10.             }
  11.             Console.ReadKey();
复制代码
回复 使用道具 举报
static void Main(string[] args)
        {
            string str = "abcdefg";
            char[] chars = str.ToCharArray();    //将字符串中每个字母都复制到一个字符数组中

        }

评分

参与人数 1技术分 +1 收起 理由
zhangcheng5468 + 1 赞一个!

查看全部评分

回复 使用道具 举报

  1. string a = "123445";
  2.             char[] b = a.ToCharArray();
  3.             foreach (var item in b)
  4.             {
  5.                 Console.WriteLine(item);
  6.             }
  7.             Console.ReadKey();
  8. 遇到这种问题,写个字符串 点 一下看看给我们提供的方法中有没有想要的就可以的,实在没有我们想要的方法再想想转换
复制代码
回复 使用道具 举报
接收的是char类型   可以看成每个字符串强转成char就行了
char[] a="字符串".ToCharArray();
回复 使用道具 举报
好的。非常感谢
回复 使用道具 举报
SUN_Q 中级黑马 2013-7-21 10:01:47
9#
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. namespace test01
  6. {
  7.     class Program
  8.     {
  9.         //怎么将一个 string =" abcdefg";的每一个字符赋给一个char[]中呢
  10.         static void Main(string[] args)
  11.         {
  12.             string a = "abcdefg";
  13.             char[] arry=new char[a.Length];
  14.             //将字符串中每个元素遍历储存到char数组
  15.             for (int i = 0; i < a.Length;i++ )
  16.             {
  17.                 arry[i]=a[i];
  18.             }
  19.             //遍历输出char数组中每个元素
  20.             for (int i = 0; i < arry.Length;i++ )
  21.             {
  22.                 Console.WriteLine("a{0}={1}",i+1,arry[i]);
  23.             }
  24.             Console.ReadKey();
  25.         }
  26.     }
  27. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马