高分求 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, j = 0; i < words.Length; i = i + 4) { arr[j++] = int.Parse(words[i]); } foreach (int n in arr) { Console.Write(n + "\t"); } } }}
[解决办法]
用 int.Parse。
但是必须满足要求
MSDN上的:
public static int Parse(string s);
s 参数包含一个如下形式的数字:
[ws][sign]digits[ws]
方括号([ 和 ])中的项是可选的;其他项的值如下所示。
ws
可选的空白。
sign
一个可选符号。
digits
一系列从 0 到 9 之间的数字。
异常:
ArgumentNullException : s 为空引用(Visual Basic 中为 Nothing)。
FormatException: s 不是仅由一个可选负号后跟一系列从 0 到 9 的数字组成的。
OverflowException : s 表示小于 MinValue 或大于 MaxValue 的数字。
[解决办法]
可以先将你的字串转化为unicode编码
string s = "您好";
string num = String.Empty; //这里用string来展示10进制任意长字串
string numx = String.Empty;//16进制
Byte[] bt = System.Text.Encoding.Unicode.GetBytes(s);
foreach (Byte b in bt)
{
num += b.ToString();
numx += b.ToString("x");
}
Response.Write(num+numx);
[解决办法]
try
{
string path1 = @"C:\Dupout";
string path2 = path1 + @"\dupout.txt";
//创建文件目录
if( !Directory.Exists( path1 ) )
{
Directory.CreateDirectory( path1 );
}
//创建文件
if( !File.Exists( path2 ) )
{
FileStream stream = File.Create( path2 );
stream.Dispose();
}
//把文本文件写入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 = 6; i < words.Length; i = i + 4 )
{
arr[ j ] = Convert.ToInt32( words[ i ] );//??????? 把words[i]转换为整数类型???? }
foreach( int n in arr )
{
Console.Write( n + "\t" );
}
}
}
catch( Exception ex )
{
Console.WriteLine( ex.Message );
}
两个错误,一个是文件流没有关闭,一个是索引位置错了。