PDA

View Full Version : Hỏi cách xử lý chuỗi



txnbac
22-08-2007, 20:57
Bạn nào bít cách viết code xử ly chuỗi trong C#.net chỉ dùm mình với

xử ly chuỗi, tách ra thành mảng các chữ cái

Cám ơn nhìu !

hatred
22-08-2007, 21:35
Bạn hỏi rõ hơn được không? Bạn muốn xử lý như thế nào?

Tienlbhoc
23-08-2007, 08:06
cái đó có sẵn trong class string rồi còn gì, giả sử tách thành mảng chữ cái nè:
string s= "abcd";
char[] c=s.ToCharArray();

txnbac
23-08-2007, 18:09
Ban chi luon mình cách làm sao chuyển 1 chuỗi tiếng việt bất kì về dang ko co dấu

VD: Học lập trình -->Chuyển thành: Hoc lap trinh

tien bac
23-08-2007, 21:29
nếu mà chữ học lập trình mà là unicode thì bạn nhanh nhất là chạy trong .net dùng 1 câu lệnh for duyệt tất cả các kí tự trong mảng char[] vừa nãy của bạn Tienlbhoc nói sau đó có các câu lệnh if nếu gặp ọ, ô, ớ, ộ ồ ờ ợ.. thì chuyển thành o tương tự như i, e,u,a cũng làm thế

samuraitruong
26-08-2007, 23:46
Dế vậy cũng hoit. dùng hàm split. chi tiết msdn. pohand.com

txnbac
28-08-2007, 10:17
Dế vậy cũng hoit. dùng hàm split. chi tiết msdn. pohand.com

Bạn nói rõ hơm về hàm split cho mình bít đc ko?

cụ thẻ code sẽ ntn?

PhamTienSInh
28-08-2007, 12:01
ac..... truy xuất tới các char trong string qua chỉ số ví dụ
Anytring[0]
Nói chung những vẫn đề quá căn bản thì vô WWW.VOVISOFT.COM mà tu đi

QuanN
03-03-2008, 07:32
Ban chi luon mình cách làm sao chuyển 1 chuỗi tiếng việt bất kì về dang ko co dấu

VD: Học lập trình -->Chuyển thành: Hoc lap trinh


/**
* Strips accents off words
*/
public static string StripDiacritics(string accented)
{
Regex regex = new Regex("\\p{IsCombiningDiacriticalMarks}+");

string strFormD = accented.Normalize(NormalizationForm.FormD);
return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
}
Tham khảo mã nguồn của VietPad.NET (http://vietpad.sf.net).

phuhoapro
30-05-2008, 11:17
e co' mot file.txt the nay.
9056_09/04/08_08:04:03_WEB_BÁN_NTP_1,000_66,000_100_66,000_6 ,600,000_19,800!09/04/08_08:04:32_100_66,000_19,800*9566_17/04/08_10:04:06_PHONE_MUA_ACB_14,100_88,200_14,100_88, 200_1,243,620,000_2,735,964!17/04/08_09:04:13_100_88,200_19,404#17/04/08_09:04:13_400_88,200_77,616#17/04/08_09:04:13_500_88,200_97,020#17/04/08_09:04:13_2,100_88,200_407,484#17/04/08_09:04:13_100_88,200_19,404#17/04/08_09:04:13_300_88,200_58,212#17/04/08_09:04:13_500_88,200_97,020#17/04/08_09:04:13_600_88,200_116,424#17/04/08_09:04:13_300_88,200_58,212#17/04/08_09:04:13_300_88,200_58,212#17/04/08_09:04:13_1,200_88,200_232,848#17/04/08_09:04:13_1,200_88,200_232,848#17/04/08_09:04:13_200_88,200_38,808#17/04/08_09:04:13_500_88,200_97,020#17/04/08_09:04:13_100_88,200_19,404#17/04/08_09:04:13_500_88,200_97,020#17/04/08_09:04:13_1,000_88,200_194,040#17/04/08_09:04:13_900_88,200_174,636#17/04/08_09:04:13_200_88,200_38,808#17/04/08_09:04:13_700_88,200_135,828#17/04/08_09:04:13_2,000_88,200_388,080#17/04/08_09:04:13_400_88,200_77,616*9582_17/04/08_10:04:31_PHONE_BÁN_NTP_10,300_61,000_2,500_61, 000_152,500,000_442,250!17/04/08_10:04:56_1,000_61,000_176,900#17/04/08_10:04:56_1,500_61,000_265,350*9671_18/04/08_08:04:08_PHONE_BÁN_PVI_9,000_43,100_9,000_43,1 00_387,900,000_1,047,330!18/04/08_08:04:33_9,000_43,100_1,047,330*9791_21/04/08_08:04:23_PHONE_BÁN_NTP_7,800_56,800_7,800_56,8 00_443,040,000_1,196,208!21/04/08_08:04:15_500_56,800_76,680#21/04/08_08:04:18_1,000_56,800_153,360#21/04/08_08:04:19_2,000_56,800_306,720#21/04/08_08:04:19_500_56,800_76,680#21/04/08_08:04:42_100_56,800_15,336#21/04/08_08:04:43_100_56,800_15,336#21/04/08_08:04:34_500_56,800_76,680#21/04/08_08:04:22_100_56,800_15,336#21/04/08_08:04:32_100_56,800_15,336#21/04/08_08:04:37_100_56,800_15,336#21/04/08_08:04:47_2,500_56,800_383,400#21/04/08_08:04:09_200_56,800_30,672#21/04/08_08:04:13_100_56,800_15,336*10039_23/04/08_08:04:02_PHONE_BÁN_ACB_14,100_82,400_14,100_82 ,400_1,161,840,000_2,556,048!23/04/08_08:04:18_500_82,400_90,640#23/04/08_08:04:06_500_82,400_90,640#23/04/08_08:04:21_400_82,400_72,512#23/04/08_08:04:21_200_82,400_36,256#23/04/08_08:04:58_1,000_82,400_181,280#23/04/08_08:04:38_10,100_82,400_1,830,928#23/04/08_08:04:43_100_82,400_18,128#23/04/08_08:04:53_500_82,400_90,640#23/04/08_08:04:45_100_82,400_18,128#23/04/08_08:04:02_100_82,400_18,128#23/04/08_08:04:04_600_82,400_108,768*10446_28/04/08_08:04:15_DIRECT_MUA_ACB_6,800_82,100_5,000_82,1 00_410,500,000_1,231,500!28/04/08_08:04:35_5,000_82,100_1,231,500*10458_28/04/08_08:04:21_DIRECT_MUA_VNC_2,400_27,000_2,400_27,0 00_64,800,000_226,800!28/04/08_08:04:19_100_27,000_9,450#28/04/08_08:04:19_500_27,000_47,250#28/04/08_08:04:19_900_27,000_85,050#28/04/08_08:04:19_900_27,000_85,050*10467_28/04/08_09:04:20_DIRECT_MUA_VNC_1,200_27,000_1,200_27,0 00_32,400,000_113,400!28/04/08_09:04:19_1,200_27,000_113,400*10468_28/04/08_09:04:43_DIRECT_MUA_VNC_200_27,000_200_27,000_5 ,400,000_18,900!28/04/08_09:04:56_200_27,000_18,900*10476_28/04/08_09:04:35_DIRECT_MUA_VNC_2,500_27,000_2,500_27,0 00_67,500,000_236,250!28/04/08_09:04:13_2,500_27,000_236,250*10478_28/04/08_09:04:40_DIRECT_MUA_VNC_500_26,800_500_26,800_1 3,400,000_46,900!28/04/08_09:04:20_500_26,800_46,900*10494_28/04/08_10:04:11_DIRECT_MUA_VNC_200_27,000_200_27,000_5 ,400,000_18,900!28/04/08_10:04:05_200_27,000_18,900*10516_28/04/08_10:04:12_DIRECT_MUA_VNC_100_27,000_100_27,000_2 ,700,000_9,450!28/04/08_10:04:49_100_27,000_9,450*10535_28/04/08_11:04:36_DIRECT_MUA_ACB_12,100_84,100_12,100_84 ,100_1,017,610,000_2,544,025!28/04/08_10:04:02_7,100_84,100_1,492,775#28/04/08_10:04:02_5,000_84,100_1,051,250*10602_29/04/08_08:04:01_WEB_MUA_DQC_2,000_79,500_2,000_76,500_ 153,000,000_413,100!29/04/08_08:04:56_2,000_76,500_413,100*
* yeu cau cua de bai..
Fan tich' va dung cai' chuoi do' thanh bang
SH Lenh| Ngay Gio| Kenh GD |Lenh Ma Ck| KL dat |Gia dat| KL Khop| Gia TB | Tong tien |Tong phi|

Khi click vao SH Lenh thi no' se show ra cac' lenh con

e da fan tich va dung dc thanh bang. Van de gap fai la kho biet cach' xu ly cai'
link: SH lenh de show ra chi tiet.
Day la code cua e:

using System.Text.RegularExpressions;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;

public partial class Default : System.Web.UI.Page
{
DataSet myDataSet;
DataTable aTable;
protected void Page_Load(object sender, EventArgs e)
{
// BuildDataSet();

}
protected void Button1_Click(object sender, EventArgs e)
{
string ret_Val = "";
ret_Val = TextBox1.Text;

if (ret_Val.Length > 50)
{
OutPut_Val(ret_Val);
}
else
{
Response.Write("<br> Không có thông tin !");
}
}

#region BuildDataSet
public void BuildDataSet()
{
//--------------Bind Table...........
myDataSet = new DataSet();
aTable = new DataTable("Table 1");
myDataSet.Tables.Add("Table 1");
//Creates and names the 12 columns data from file 2cylinintegers.dat and adds to Table 1
DataColumn Column1 = new DataColumn("SH Lenh");
DataColumn Column2 = new DataColumn("Ngay");
DataColumn Column3 = new DataColumn("Gio");
DataColumn Column4 = new DataColumn("Kenh GD");
DataColumn Column5 = new DataColumn("Lenh");
DataColumn Column6 = new DataColumn("MaCk");
DataColumn Column7 = new DataColumn("KL Dat");
DataColumn Column8 = new DataColumn("Gia Dat");
DataColumn Column9 = new DataColumn("KL Khop");
DataColumn Column10 = new DataColumn("Gia TB");
DataColumn Column11 = new DataColumn("Tong tien");
DataColumn Column12 = new DataColumn("Tong phi");

myDataSet.Tables["Table 1"].Columns.Add(Column1);
myDataSet.Tables["Table 1"].Columns.Add(Column2);
myDataSet.Tables["Table 1"].Columns.Add(Column3);
myDataSet.Tables["Table 1"].Columns.Add(Column4);
myDataSet.Tables["Table 1"].Columns.Add(Column5);
myDataSet.Tables["Table 1"].Columns.Add(Column6);
myDataSet.Tables["Table 1"].Columns.Add(Column7);
myDataSet.Tables["Table 1"].Columns.Add(Column8);
myDataSet.Tables["Table 1"].Columns.Add(Column9);
myDataSet.Tables["Table 1"].Columns.Add(Column10);
myDataSet.Tables["Table 1"].Columns.Add(Column11);
myDataSet.Tables["Table 1"].Columns.Add(Column12);

StreamReader myReader = new StreamReader(@"c:\demo.txt");
//Checks to see if the Reader has reached the end of the stream
while (myReader.Peek() > -1)
{
//Reads a line of data from the data file
string myString = myReader.ReadLine().Trim();
//Uses Regex.Split method to create an array of strings that represents each entry in the line.
string[] split = System.Text.RegularExpressions.Regex.Split(myStrin g, "\\s+", RegexOptions.None);

//Adds the array as a new DataRow to Table 1
myDataSet.Tables["Table 1"].Rows.Add(split);

// DataRow theRow = myDataSet.Tables["Table 1"].NewRow();
// theRow["SH Lenh"] = (decimal)split[0];
//theRow["Ngay"] = (decimal)split[1];
//theRow["Gio"] = (decimal)split[2];
//theRow["Kenh GD"] = (decimal)split[3];
//theRow["Lenh"] = (decimal)split[4];
//theRow["MaCk"] = (decimal)split[5];
//theRow["KL Dat"] = (decimal)split[6];
//theRow["Gia Dat"] = (decimal)split[7];
//theRow["KL Khop"] = (decimal)split[8];
//theRow["Gia TB"] = (decimal)split[9];
//theRow["Tong tien"] = (decimal)split[10];
//theRow["Tong phi"] = (decimal)split[11];

// myDataSet.Tables["Table 1"].Rows.Add(theRow);
}
MyGrid.DataSource = myDataSet.Tables["Table 1"];
MyGrid.DataSource = myDataSet;

//Rows = input_String.Split('*');
////Rows = input_String.StartsWith('*');
//for (Idx = 0; Idx <= Rows.GetUpperBound(0) - 1; Idx++)
//{
// Pos = Rows[Idx].IndexOf('!');
// Master[0] = Rows[Idx].Substring(0, Pos);
// Master[1] = Rows[Idx].Substring(Pos + 1, Rows[Idx].Length - Pos - 1);

// {
// tmp_Str = "";
// Val = Master[0].Split('_');
// for (Child_Idx = 0; Child_Idx <= Val.GetUpperBound(0); Child_Idx++)
// {
// tmp_Str = tmp_Str + " &nbsp; &nbsp; &nbsp; &nbsp;" + Val[Child_Idx];
// //Response.Write("XX"+Val[Child_Idx].ToString());//fan tu cuoi cung cua !
// }

// Response.Write("<br>" + tmp_Str);
// }

// Child = Master[1].Split('#');

//for (Master_Idx = 0; Master_Idx <= Child.GetUpperBound(0); Master_Idx++)
//{
// tmp_Str = "";
// Val = Child[Master_Idx].Split('_');
// for (Child_Idx = 1; Child_Idx <= Val.GetUpperBound(0); Child_Idx++)
// {
// tmp_Str = tmp_Str + " &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; " + Val[Child_Idx];
// // Response.Write("**");//cac' fan tu gia' gtri cua !
// }

// Response.Write("<br>" + tmp_Str);


//}




//-------------------------------end process..........
}
#endregion


private void OutPut_Val(string input_String)
{
DataTable TableName = new DataTable();
// string columns = input_String.Split(remove.ToCharArray());
//result.Tables.Add(TableName);

//stringbuild.Append("");
string[] Master = new string[2];
string[] Child;
string[] Rows;
string[] Val;
string tmp_Str;
int Pos;
byte Idx;
byte Master_Idx;
byte Child_Idx;
string outs = "";
string tmp_Date = "";
Response.Write("<br> Chi tiết lệnh: ");
Response.Write("<br>");

Response.Write("&nbsp; &nbsp; &nbsp; &nbsp; SH Lenh &nbsp; &nbsp; Ngay &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; Gio &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; Kenh GD &nbsp; &nbsp; &nbsp; Lenh &nbsp; &nbsp; &nbsp; Ma CK &nbsp; &nbsp; KL dat &nbsp; &nbsp; Gia dat &nbsp; &nbsp; KL khop &nbsp; &nbsp; Gia TB &nbsp; &nbsp; &nbsp; &nbsp; Tong tien &nbsp; &nbsp; Tong phi");

// foreach(string cols in columns)
if (input_String.Length < 10)
return;

Rows = input_String.Split('*');
for (Idx = 0; Idx <= Rows.GetUpperBound(0) - 1; Idx++)
{
Pos = Rows[Idx].IndexOf('!');
Master[0] = Rows[Idx].Substring(0, Pos);
Master[1] = Rows[Idx].Substring(Pos + 1, Rows[Idx].Length - Pos - 1);

{
tmp_Str = "";
Val = Master[0].Split('_');

for (Child_Idx = 0; Child_Idx <= Val.GetUpperBound(0); Child_Idx++)
{
tmp_Str = tmp_Str + " &nbsp; &nbsp; &nbsp; &nbsp;" + Val[Child_Idx];
//Response.Write("XX"+Val[Child_Idx].ToString());//fan tu cuoi cung cua !
}

Response.Write("<br>" + tmp_Str);
}

Child = Master[1].Split('#');

for (Master_Idx = 0; Master_Idx <= Child.GetUpperBound(0); Master_Idx++)
{
tmp_Str = "";
Val = Child[Master_Idx].Split('_');
for (Child_Idx = 1; Child_Idx <= Val.GetUpperBound(0); Child_Idx++)
{
tmp_Str = tmp_Str + " &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; " + Val[Child_Idx];
//Response.Write(Val[Child_Idx]);//cac' fan tu gia' gtri cua !
// hpLink.Text = tmp_Str + Val[Child_Idx].ToString();
}
Response.Write("<br>" + tmp_Str);
}
}
//----------------------------//
// create a string type variable to generate dynamic table

string dynTable = "";

// start with table tag with following attributes

dynTable = "<table cellspacing=\"0\" cellpadding=\"2\" border=\"1\">";
dynTable += "<tr>";

dynTable += "<td>SH Lenh";
dynTable += "</td>";
dynTable += "<td>Ngay";
dynTable += "</td>";

dynTable += "<td>Gio";
dynTable += "</td>";

dynTable += "<td>Kenh GD";
dynTable += "</td>";

dynTable += "<td>Lenh";
dynTable += "</td>";

dynTable += "<td>Ma Ck";
dynTable += "</td>";

dynTable += "<td>KL dat";
dynTable += "</td>";

dynTable += "<td>Gia dat";
dynTable += "</td>";

dynTable += "<td>KL Khop";
dynTable += "</td>";

dynTable += "<td>Gia TB";
dynTable += "</td>";

dynTable += "<td>Tong tien";
dynTable += "</td>";

dynTable += "<td>Tong phi";
dynTable += "</td>";
dynTable += "<td>Chi tiet";
dynTable += "</td>";
dynTable += "</tr>";
// outer loop to generate table rows
for (Idx = 0; Idx <= Rows.GetUpperBound(0) - 1; Idx++)
{
dynTable += "<tr>";
Pos = Rows[Idx].IndexOf('!');
Master[0] = Rows[Idx].Substring(0, Pos);
Master[1] = Rows[Idx].Substring(Pos + 1, Rows[Idx].Length - Pos - 1);
//Response.Write(Master[0].ToString()+"<br>");
Response.Write(Rows[0].ToString());
tmp_Str = "";
Val = Master[0].Split('_');
if (Val[1] == tmp_Date)
{
Val[1] = " &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; ";
}

else
{
tmp_Date = Val[1];
}

for (Child_Idx = 0; Child_Idx <= Val.GetUpperBound(0); Child_Idx++)
{
///tmp_Str = tmp_Str + " &nbsp; &nbsp; &nbsp; &nbsp;" + Val[Child_Idx];
//Response.Write("XX"+Val[Child_Idx].ToString( ));//fan tu cuoi cung cua !

dynTable += "<td>";
dynTable += Val[Child_Idx];
// close td column tag
dynTable += "</td>";
}
dynTable += "<td>";
dynTable += "<a href=\"Default_Detail.aspx\">";

dynTable += "Chi tiet";
dynTable += "</a>";
dynTable += "</td>";
dynTable += "</tr>";
Response.Write("<br>" + tmp_Str);
}

/* for (Master_Idx = 0; Master_Idx <= Child.GetUpperBound(0); Master_Idx++)
{
tmp_Str = "";
Val = Child[Master_Idx].Split('_');
for (Child_Idx = 1; Child_Idx <= Val.GetUpperBound(0); Child_Idx++)
{
// tmp_Str = tmp_Str + " &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; " + Val[Child_Idx];
// Response.Write("**");//cac' fan tu gia' gtri cua !
//hpLink.Text = Val[Child_Idx];
}

Response.Write("<br>" + tmp_Str);

}
* Click vao chi tiet.. no' se hien thi doan code nay... tuong ung voi tung ma lenh.

*/ Child = Master[1].Split('#');
// close the table tag

dynTable += "</table>";
Literal1.Text = dynTable;

}

}
Day la fan code fia' HTML

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Label ID="_message" runat="server"
Text="" Visible="false"></asp:Label>

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /><br />
<br />
&nbsp;<br />
<asp:GridView ID="MyGrid" runat="server" AutoGenerateColumns="False" Width="303px">
</asp:GridView>
<asp:Label ID="Literal1" runat="server" Text="Label"></asp:Label>
<br />

</div>

</form>
</body>
</html>

Cao thu nao biet giup e fat'... fan tich' may' cai' nay khoai wa........

[=========> Bổ sung bài viết <=========]

chuoi cua e la cac' lenh con cua thang (SH Lenh)
vi' du: lenh:
9566 17/04/08 10:04:06 PHONE MUA ACB 14,100 88,200 14,100 88,200 1,243,620,000 2,735,964
con cua no' co' cac ma sau:
09:04:13 100 88,200 19,404
09:04:13 400 88,200 77,616
09:04:13 500 88,200 97,020
09:04:13 2,100 88,200 407,484
09:04:13 100 88,200 19,404
09:04:13 300 88,200 58,212
09:04:13 500 88,200 97,020
09:04:13 600 88,200 116,424
09:04:13 300 88,200 58,212
09:04:13 300 88,200 58,212
09:04:13 1,200 88,200 232,848
09:04:13 1,200 88,200 232,848
09:04:13 200 88,200 38,808
09:04:13 500 88,200 97,020
09:04:13 100 88,200 19,404
09:04:13 500 88,200 97,020
09:04:13 1,000 88,200 194,040
09:04:13 900 88,200 174,636
09:04:13 200 88,200 38,808
09:04:13 700 88,200 135,828
09:04:13 2,000 88,200 388,080
09:04:13 400 88,200 77,616

tuannqt
30-05-2008, 11:37
Ban chi luon mình cách làm sao chuyển 1 chuỗi tiếng việt bất kì về dang ko co dấu

VD: Học lập trình -->Chuyển thành: Hoc lap trinh

Nếu muốn làm như vậy bạn phải có 1 thư viện chuyển mã tiếng VIệt,
Hoặc phải tự viết class tiện ích để làm việc trên

Vì vấn đề có liên quan đến ngôn ngữ tiếng Việt, nên không thể giải quyết được bằng những giải thuật xử lý chuỗi đơn giản và được hỗ trợ sẵn

Mình đưa ra trường hợp đơn giản là mã cho chữ Đ trong bảng mã Unicode có tại 2 vị trí, giải quyết làm sao?!?!
Nhất thời thì mình cũng chưa thể đưa ra được giải pháp cho bạn.
Bạn có thể vào đây để thảo luận thêm: http://Codeprovn.com