我在该表格中有一个表格 Reviws 我有专栏说明 所以用户可以添加说明。现在我需要的是描述段落需要忽略一些单词和显示。
我有一些像
这样的词 a
is
about
above
after
again
against
all
这是示例段落。
阿努拉德普勒是斯里兰卡的一个主要城市。它是斯里兰卡中北部省份和阿努拉德普勒区的首府。
但我希望只显示一些像这样的话
阿努拉德普勒主要城市斯里兰卡首都城市中北部,斯里兰卡首都阿努拉德普勒区。
这就是现在我如何显示我的描述
Review.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string constr = "Data Source=(localdb)\\ProjectsV13;Initial Catalog=ReviewDB;Integrated Security=True";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT Description, Place FROM Reviews"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
Review.aspx
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true"
OnPageIndexChanging="OnPaging" PageSize="10">
<Columns>
<asp:BoundField ItemStyle-Width="500px" DataField="Description" HeaderText="Description" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
答案 0 :(得分:0)
如果您的单词仅以空格分隔,这应该很简单:
var DescWords = Description.Split(" ".ToCharArray());
//WordsToRemove is the list of words that you don't want to show.
var NewDesc = String.Join(" ", DescWords.Except(WordsToRemove));
确保在文件顶部添加using System.Linq;
。另请注意,您需要将Description
替换为要修复的实际DataTable
值的名称。例如,这可能是dt[0]["Description"]
或其他内容。
如果您的描述中包含其他标点符号,则会更加复杂,但仍可轻松实现。
如果您的字符串包含多个分隔符,您可以像这样处理它:
var WordsToRemove = new[] { "a", "is", "about", "above", "after", "again", "against", "all" };
var Delimiters = ".,;: ()!?/";
int LastIndex = 0;
var NewDesc = "";
for (int row = 0; row < dt.Rows.Count; row++)
{
var Description = dt.Rows[row]["Description"].ToString();
for (int i = 0; i < Description.Length; i++)
{
if (Delimiters.IndexOf(Description[i]) >= 0)
{
var Word = Description.Substring(LastIndex, i - LastIndex + 1);
if (WordsToRemove.Contains(Word.Trim(Delimiters.ToCharArray())))
NewDesc += Description[i];
else
NewDesc += Word;
LastIndex = i + 1;
}
}
}
在代码中的sda.Fill(dt);
行下方注入此代码。在现实世界中,这样的问题稍有不同地解决,例如,您应该考虑将此代码移动到一个函数中,另外您应该使用StringBuilder
代替string
,但由于您是初学者,我只是为您留下提示。
答案 1 :(得分:0)
访问DataTable
的内容:
string myData=dt.Rows[0][1].ToString();
为字符串的循环创建一个循环以删除所需的单词
string bannedWords = @"\b(this|is|the|list|of|banned|words)\b";
foreach(data in myData)
string replaced = Regex.Replace(data, bannedWords, "", RegexOptions.IgnoreCase);
然后在GridView
答案 2 :(得分:0)
这是用于删除字符串中不需要的单词的代码段
string description = "Anuradhapura is a major city in Sri Lanka. It is the capital city of North Central Province, Sri Lanka and the capital of Anuradhapura District";
List<string> ignoredWords = new List<string>() { "a","is","about","above","after","again","against","all", "just", "text","of","and","it","the" };
description = string.Join(" ", description.Split().Where(words => !ignoredWords.Contains(words, StringComparer.InvariantCultureIgnoreCase)));
<强>输出强>
Anuradhapura major city in Sri Lanka. capital city North Central Province, Sri Lanka capital Anuradhapura District
现在,根据您的要求,我已合并代码以提供完整的答案。
在这里,我创建了一个示例DataTable
,而您从Database
获得了该示例,并且我已删除了分页,请将其添加到您的代码中。
<强> ASPX 强>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true" OnPageIndexChanging="OnPaging" PageSize="10" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField ItemStyle-Width="500px" DataField="Description" HeaderText="Description" />
</Columns>
我在这里添加了OnRowDataBound
个事件。
<强> CS 强>
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string constr = "Data Source=(localdb)\\ProjectsV13;Initial Catalog=ReviewDB;Integrated Security=True";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT Description, Place FROM Reviews"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
//In `RowDataBound` I'm doing all the manipulations
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView drv = (DataRowView)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (drv["Description"] != DBNull.Value)
{
string description = Convert.ToString(drv["Description"]);
e.Row.Cells[0].Text = RemoveUnwanedWords(description);
}
}
}
public string RemoveUnwanedWords(string description)
{
List<string> ignoredWords = new List<string>() { "a", "is", "about", "above", "after", "again", "against", "all", "just", "text", "of", "and", "it", "the" };
return string.Join(" ", description.Split().Where(words => !ignoredWords.Contains(words, StringComparer.InvariantCultureIgnoreCase)));
}