我试过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");
}
下面是我的代码
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");
}
if(Int32.TryParse(words[i],temp))
arr[j] = temp;
if(Int32.TryParse(words[i],temp))
arr[j] = temp;
for (int i = 8; i < words.Length; i = i + 4)
{
arr[j] = Convert.ToInt32(words[i]);//??????? 把words[i]转换为整数类型????
}
如果把int i = 8;改成 int i=6则可以取出年龄的值。
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");
}
}
}
}就是循环搞错了
前者在失败的时候抛异常,后者不抛
但是必须满足要求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 的数字。
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);
{
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 );
}两个错误,一个是文件流没有关闭,一个是索引位置错了。
public bool IsDecimal(string strNum)
{
try
{
decimal var = Convert.ToDecimal(strNum);
return true;
}
catch
{
return false;
}
}
code:string[] words = text.Split('\t', '\n'); int[] arr = new int[4];
for (int i = 7, j = 0; i < words.Length; i = i + 4)
{
if(IsDecimal(words[i].tostring()))
arr[j++] = int.Parse(words[i]);
}
foreach (int n in arr)
{
Console.Write(n + "\t");
}
int32.tryParse