关于字符串搜索的问题?

jjyjjyjjy 2006-02-16 11:22:08
我想做一个类似于金山词霸的字符串搜索的界面,但没什么好的字符串搜索算法,实现效果不理想,请问各位老大有什么高见?
...全文
108 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
杨哥儿 2006-02-17
  • 打赏
  • 举报
回复
有了.
dolfen 2006-02-16
  • 打赏
  • 举报
回复
使用DataTable的Select()方法:
例如(关键代码):
private void searching()
{
DataRow[] drArr=words.Select("word like '" + txtInput.Text + "%'");
if(drArr.Length>0)
{
lstWords.SelectedIndex=Convert.ToInt32(drArr[0]["index"]);
}
}

演示程序全部代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication6
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox lstWords;
private System.Windows.Forms.TextBox txtInput;
private System.Windows.Forms.TextBox txtMeans;
DataTable words=new DataTable();
private System.Windows.Forms.Label label1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();

//导入词典
words.Columns.Add("index",typeof(long));
words.Columns.Add("word",typeof(string));
words.Columns.Add("means",typeof(string));
//....
//lstWords.DataSource=words.DefaultView;

//(以下是生成测试数据)
lstWords.Items.Clear();
string tmp="abcdefghijklmnopqrstuvwxyz";
string str01="",str02="";
for(int k=0; k<26; k++)
{
str01=tmp.Substring(k%26,1);
for(int j=0; j<26; j++)
{
str02=tmp.Substring(j%26,1);
for(int i=0; i<26; i++)
{
object[] rowVals = new object[3];
rowVals[0]=i+j*26+k*26*26;
rowVals[1]= str01 + str02 + tmp.Substring(i%26,1);
rowVals[2]="【"+rowVals[1].ToString()+"】意思是"+rowVals[0].ToString();
words.Rows.Add(rowVals);
lstWords.Items.Add(rowVals[1].ToString());
}
}
}

}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.lstWords = new System.Windows.Forms.ListBox();
this.txtInput = new System.Windows.Forms.TextBox();
this.txtMeans = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// lstWords
//
this.lstWords.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)));
this.lstWords.ItemHeight = 12;
this.lstWords.Location = new System.Drawing.Point(8, 40);
this.lstWords.Name = "lstWords";
this.lstWords.Size = new System.Drawing.Size(200, 316);
this.lstWords.TabIndex = 0;
this.lstWords.SelectedIndexChanged += new System.EventHandler(this.lstWords_SelectedIndexChanged);
//
// txtInput
//
this.txtInput.Location = new System.Drawing.Point(88, 8);
this.txtInput.Name = "txtInput";
this.txtInput.Size = new System.Drawing.Size(384, 21);
this.txtInput.TabIndex = 1;
this.txtInput.Text = "";
this.txtInput.TextChanged += new System.EventHandler(this.txtInput_TextChanged);
//
// txtMeans
//
this.txtMeans.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Right)));
this.txtMeans.Location = new System.Drawing.Point(216, 40);
this.txtMeans.Multiline = true;
this.txtMeans.Name = "txtMeans";
this.txtMeans.Size = new System.Drawing.Size(256, 320);
this.txtMeans.TabIndex = 2;
this.txtMeans.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(32, 12);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(48, 16);
this.label1.TabIndex = 3;
this.label1.Text = "单词:";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(480, 365);
this.Controls.Add(this.label1);
this.Controls.Add(this.txtMeans);
this.Controls.Add(this.txtInput);
this.Controls.Add(this.lstWords);
this.Name = "Form1";
this.Text = "Dolfen词典演示";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void searching()
{
DataRow[] drArr=words.Select("word like '" + txtInput.Text + "%'");
if(drArr.Length>0)
{
lstWords.SelectedIndex=Convert.ToInt32(drArr[0]["index"]);
}
}

private void txtInput_TextChanged(object sender, System.EventArgs e)
{
searching();
}

private void lstWords_SelectedIndexChanged(object sender, System.EventArgs e)
{
txtMeans.Text=words.Rows[lstWords.SelectedIndex]["means"].ToString();
}
}
}
timiil 2006-02-16
  • 打赏
  • 举报
回复
需求能否说得清楚些?

17,748

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧