用split分割: public class SplitTest { public static void Main() { string words = "this is a list of words, with: a bit of punctuation."; string [] split = words.Split(new Char [] {' '}); foreach (string s in split) { if (s.Trim() != "") Console.WriteLine(s); } } }
/// <summary> /// 得到文件内容 /// </summary> /// <param name="TextFilePath">文件路径</param> /// <returns>文件内容字符串</returns> public static string IO_GetFileContent(string TextFilePath) { FileStream stream1 = new FileStream(TextFilePath, FileMode.Open, FileAccess.Read); byte[] buffer1 = new byte[(int)stream1.Length]; stream1.Read(buffer1, 0, buffer1.Length); stream1.Close(); return Encoding.Default.GetString(buffer1); }//--操作部分 string a = IO_GetFileContent("d:\\test.txt"); string[] ar = a.Split('\n'); string[] dataArr = new string[3];//每行的数据数组 int i = 0; for (int ix = 0; ix < ar.Length; ix++) { string ax = ar[ix]; string[] da = ar[ix].Split(' '); for(int nx = 0; nx< da.Length; nx++) { if(da[nx] != "") { dataArr[i] = da[nx]; i++; if (i > 2) i = 0; } } UpDataBase(dataArr);//你定义的数据更新函数如 private void UpDataBase(string[] _DATA) }
using System; using System.IO; using System.Text.Encoding; using System.Text.RegularExpressions;public class MyClass { public static void Main() { StreamReader sr = new StreamReader(yourPath, Encoding.Default); string array = null;//临时数组 string tempStr;//临时变量 Regex regex = new Regex("\\s+");//正则表达式实例 while((tempStr = sr.ReadLine()) != null)//每次读一行 { array = regex.Split(tempStr);//使用正则表达式分割 if(array == null || array.Length != 3)//如果没有或者分割后数组长度不是3 { continue;//读下一行 } //以下是分割后的情况 array[0];//1 array[1];//张三 array[2];//123456 } sr.Close(); }
}
//完整代码如下, using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Text.RegularExpressions; using System.Xml.Xsl; using System.Xml; using System.Configuration; using System.Data; using System.Diagnostics;//http://blog.csdn.net/zhzuonamespace Zhzuo.VS2005Test.ConsoleTest { class Program {
public class SplitTest {
public static void Main() { string words = "this is a list of words, with: a bit of punctuation."; string [] split = words.Split(new Char [] {' '}); foreach (string s in split) { if (s.Trim() != "")
Console.WriteLine(s);
}
}
}
/// 得到文件内容
/// </summary>
/// <param name="TextFilePath">文件路径</param>
/// <returns>文件内容字符串</returns>
public static string IO_GetFileContent(string TextFilePath)
{
FileStream stream1 = new FileStream(TextFilePath, FileMode.Open, FileAccess.Read);
byte[] buffer1 = new byte[(int)stream1.Length];
stream1.Read(buffer1, 0, buffer1.Length);
stream1.Close();
return Encoding.Default.GetString(buffer1);
}//--操作部分
string a = IO_GetFileContent("d:\\test.txt");
string[] ar = a.Split('\n');
string[] dataArr = new string[3];//每行的数据数组
int i = 0;
for (int ix = 0; ix < ar.Length; ix++)
{
string ax = ar[ix];
string[] da = ar[ix].Split(' ');
for(int nx = 0; nx< da.Length; nx++)
{
if(da[nx] != "")
{
dataArr[i] = da[nx];
i++;
if (i > 2)
i = 0;
}
}
UpDataBase(dataArr);//你定义的数据更新函数如 private void UpDataBase(string[] _DATA)
}
//一行一行的读,用循环添加
string[] lines=(从文本中读取的一行数据").split(' ');
al.add(lines);
//end
ArrayList AL=new ArrayList();
foreach(string[] str in al)
{
string uptxt="";
foreach(string tx in str)
{
if(tx!="" && tx!=null && tx!=" ")
{
uptxt+=tx+",";
}
}
string[] updatabase=uptxt.split(',');
AL.add(updatabase);
}
(本人还是菜鸟,写错了不要见怪!!)
using System.IO;
using System.Text.Encoding;
using System.Text.RegularExpressions;public class MyClass
{
public static void Main()
{
StreamReader sr = new StreamReader(yourPath, Encoding.Default);
string array = null;//临时数组
string tempStr;//临时变量
Regex regex = new Regex("\\s+");//正则表达式实例
while((tempStr = sr.ReadLine()) != null)//每次读一行
{
array = regex.Split(tempStr);//使用正则表达式分割
if(array == null || array.Length != 3)//如果没有或者分割后数组长度不是3
{
continue;//读下一行
}
//以下是分割后的情况
array[0];//1
array[1];//张三
array[2];//123456
}
sr.Close();
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml.Xsl;
using System.Xml;
using System.Configuration;
using System.Data;
using System.Diagnostics;//http://blog.csdn.net/zhzuonamespace Zhzuo.VS2005Test.ConsoleTest
{
class Program
{
static void Main(string[] args)
{
//data.Txt 文本内容如下
//1 张三 123456
//2 李四 654321
//3 王五 345123
//
DataTable dt = CreateDateTable();
using(StreamReader reader = new StreamReader("c:\\data.txt", Encoding.Default))
{
string s = reader.ReadLine();
while (String.IsNullOrEmpty(s) == false)
{
string[] array = Regex.Split(s, @"\s+");//s.Split(new char[] { ' '});
if (array != null && array.Length == 3)
{
dt.LoadDataRow(array, true);
}
s = reader.ReadLine();
}
}
UpdateDataToDB( dt);
System.Console.ReadLine();
} static DataTable CreateDateTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("ID",typeof(string));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Code", typeof(string)); return dt;
} static void UpdateDataToDB(DataTable dt)
{
//编写更新到数据库代码
}
}
}