高分求 c# 字符串转 换成 整数类型 的方法
我试过C# vs.net上所有的将字符串转换为整数型的方法 就是不行啊!! 请大家帮帮忙!!急啊!!
下面是我的代码
string path1 = @"e:\Dupout";
string path2 =path1+ @"\dupout.txt";
//创建文件目录
if (!Directory.Exists(path1))
{
Directory.CreateDirectory(path1);
}
//创建文件
if (!File.Exists(path2))
{
File.Create(path2);
}
//把文本文件写入dupout.txt文件
using (StreamWriter sw = File.CreateText(path2))
{
sw.WriteLine("姓名\t性别\t年龄\t工资\t");
sw.WriteLine("刘洋成\t男\t18\t2354\t");
sw.WriteLine("敖翔\t男\t19\t3456\t");
sw.WriteLine("陈玲\t女\t18\t1234\t");
sw.WriteLine("杜贤伟\t男\t20\t4456\t");
}
string text = null;
using (StreamReader sr = File.OpenText(path2))//path2= @"e:\Dupout\dupout.txt";
{
while (sr.Peek()>=0)
{
text+=sr.ReadLine();//把文件中的数据读入text
}
}
string[] words = text.Split('\t','\n');
int[] arr = new int[4];
int j=0;
for (int i = 8; i < words.Length; i = i + 4)
{
arr[j] = Convert.ToInt32(words[i]);//??????? 把words[i]转换为整数类型???? }
foreach (int n in arr)
{
Console.Write(n + "\t");
}
------解决方案--------------------LZ是不是把循环的索引给搞错了?
for (int i = 8; i < words.Length; i = i + 4)
{
arr[j] = Convert.ToInt32(words[i]);//??????? 把words[i]转换为整数类型????
}
如果把int i = 8;改成 int i=6则可以取出年龄的值。
------解决方案--------------------C# code
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace ConsoleApplication69
{
class Program
{
static void Main(string[] args)
{
string path1 = @"e:\Dupout";
string path2 = path1 + @"\dupout.txt";
//创建文件目录
if (!Directory.Exists(path1))
{
Directory.CreateDirectory(path1);
}
//创建文件
if (!File.Exists(path2))
{
File.Create(path2);
}
//把文本文件写入dupout.txt文件
using (StreamWriter sw = File.CreateText(path2))
{
sw.WriteLine("姓名\t性别\t年龄\t工资\t");
sw.WriteLine("刘洋成\t男\t18\t2354\t");
sw.WriteLine("敖翔\t男\t19\t3456\t");
sw.WriteLine("陈玲\t女\t18\t1234\t");
sw.WriteLine("杜贤伟\t男\t20\t4456\t");
}
string text = null;
using (StreamReader sr = File.OpenText(path2))//path2= @"e:\Dupout\dupout.txt";
{
while (sr.Peek() >= 0)
{
text += sr.ReadLine();//把文件中的数据读入text
}
}
string[] words = text.Split('\t', '\n');
int[] arr = new int[4];
for (int i = 7,