.net里面怎么把 String型数据转换成 char[]?谢谢

ahclx 2005-10-04 09:40:29
用c编的程序,移植到.net里面 赋值时需要进行String 和 char[] 互相转换 但用了很多函数系统都不能编译
哪位前辈给我帮帮忙 谢了
能写点详细的代码嘛?包括变量的申明,因为我真的是没自信了,怎么改都出错……

谢了
ps:这是我第一次发问题,同学介绍来的……汗 我新的厉害……
...全文
318 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
the777 2005-10-05
  • 打赏
  • 举报
回复
#include <iostream>
#include <tchar.h>

#using <mscorlib.dll>

using namespace System;

int _tmain()
{
String* name = Console::ReadLine();
__wchar_t ch __gc[] = name->ToCharArray();
for (int i = 0; i < name->Length; ++i)
{
Console::WriteLine(ch[i]);
}

return 0;
}
the777 2005-10-05
  • 打赏
  • 举报
回复
mathsword(梦在流浪)老兄,你写的是C#程序呀???
ahclx 2005-10-05
  • 打赏
  • 举报
回复
谢谢 已经解决这个问题了 不过新问题又来
我又问了个~~
ahclx 2005-10-05
  • 打赏
  • 举报
回复
谢谢
现在已经成功把String型转为char[]了。
昨天看了回复后开始试着写C#
但我现在在C#.net里面,不知道怎么把char[]换回String……
我看过arr[]里面的每个字符,已经成功的由String转换过来了。
但用str=System.Convert.ToString(arr);时
str显示的是:System.Char[]
我不明白为什么。还要麻烦你们,谢了
ahclx 2005-10-04
  • 打赏
  • 举报
回复
上面的代码不能编译……
我用的是C++.net
能改的都改了下 不过还是不行
mathsword 2005-10-04
  • 打赏
  • 举报
回复
using System;

class Sample {
public static void Main() {
string str = "012wxyz789";
char[] arr;

arr = str.ToCharArray(3, 4);
Console.Write("The letters in '{0}' are: '", str);
Console.Write(arr);
Console.WriteLine("'");
Console.WriteLine("Each letter in '{0}' is:", str);
foreach (char c in arr)
Console.WriteLine(c);
}
}
/*
This example produces the following results:
The letters in '012wxyz789' are: 'wxyz'
Each letter in '012wxyz789' is:
w
x
y
z
*/
fanruinet 2005-10-04
  • 打赏
  • 举报
回复
String.ToCharArray()
或用
System.Text.Encoding.Default.GetBytes(string)
ahclx 2005-10-04
  • 打赏
  • 举报
回复
谁来帮我啊 好难办的 头都大了 在线等贵人相助~

7,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 VC.NET
社区管理员
  • VC.NET社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧