Count the string by words not char - c#

I am working on this code in which it can identify the word listed below from the Web Browser. Those words will turn into asterisk once they are identified and will count how many words were replaced but it didn't work. Someone can help?
Here is my code:
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(txbAdress.Text);
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
IHTMLDocument2 doc2 = webBrowser1.Document.DomDocument as IHTMLDocument2;
StringBuilder html = new StringBuilder(doc2.body.outerHTML);
string query;
query = #"
select Word from ListWords
";
List<string> words = new List<string>();
DataSet ds;
DataRow drow;
ds = DatabaseConnection.Connection1(query);
int index, total;
total = ds.Tables[0].Rows.Count;
string current_word;
for (index = 0; index < total; index++ )
{
drow = ds.Tables[0].Rows[index];
current_word = drow.ItemArray.GetValue(0).ToString();
words.Add(current_word);
}
Console.WriteLine(query);
Console.WriteLine("array:" + words);
foreach (String key in words)
{
// String substitution = "<span style='background-color: rgb(255, 0, 0);'>" + key + "</span>";
int len = key.Length;
string replace = "";
for ( index = 0; index < len; index++)
{
replace += "*";
}
html.Replace(key, replace);
//count++;
}
//Console.WriteLine("Total number of words: " + count);
doc2.body.innerHTML = html.ToString();
}

Just count them before you replace them
Given
public int CountOccurrences(string source, string match)
{
var pos = 0;
var count = 0;
while ((pos < source.Length) && (pos = source.IndexOf(match, pos, StringComparison.Ordinal)) != -1)
{
count++;
pos += match.Length;
}
return count;
}
Example
var count = CountOccurrences(html,key);
html.Replace(key, substitution);

Related

How to highliht specific word in richtextbox during writing?

Basically I want to make an autocorrect for my language. I have a RichTextBox(name: MainText), where I write. during writing, the program should every word if it exists in a dictionary file. if not then change the specific word color to red.
It has a timer. after every second it gets the written text and puts the words to an str array, and reads correct words from dictionary.txt file and puts them in a list. during comparison of the strings, it never highlights the incorrect words and it has always indexOutOfRange errors. How to fix it?
Here is the timer tick void:
void CheckTimer_Tick(object sender, EventArgs e)
{
List<string> correct_words = File.ReadAllLines(DictPath).ToList();
string text = MainText.Text;
string[] Words = text.Split(' ', '.');
for (int i = 0; i < Words.Length; i++)
{
if (correct_words.Contains(Words[i])) { }
else
{
int index = 0;
String temp = MainText.Text;
MainText.Text = "";
MainText.Text = temp;
while (index < MainText.Text.LastIndexOf(Words[i]))
{
MainText.Find(Words[i], index, MainText.TextLength, RichTextBoxFinds.None);
MainText.SelectionColor = Color.Red;
index = MainText.Text.IndexOf(Words[i], index) + 1;
}
}
}
}
I also tried this void:
void HighlightPhrase(RichTextBox box, string phrase, Color color)
{
int pos = box.SelectionStart;
string s = box.Text;
for (int ix = 0; ;)
{
int jx = s.IndexOf(phrase, ix, StringComparison.CurrentCultureIgnoreCase);
if (jx < 0) break;
box.SelectionStart = jx;
box.SelectionLength = phrase.Length;
box.SelectionColor = color;
ix = jx + 1;
}
box.SelectionStart = pos;
box.SelectionLength = 0;
box.ForeColor = Color.Black;
}
This worked if I gave it a specific string, but it couldn't recognize the phrase input from the dictionary and gave the indexOutOfRange error.
public Form1()
{
InitializeComponent();
}
private void textbox1_TextChanging(object sender, EventArgs e)
{
string[] words = textBox1.Text.Split(',');
foreach(string word in words)
{
int startindex = 0;
while(startindex < richTextBox1.TextLength)
{
int wordstartIndex = richTextBox1.Find(word, startindex, RichTextBoxFinds.None);
if (wordstartIndex != -1)
{
richTextBox1.SelectionStart = wordstartIndex;
richTextBox1.SelectionLength = word.Length;
richTextBox1.SelectionBackColor = Color.Yellow;
}
else
break;
startindex += wordstartIndex + word.Length;
}
}
}
For Clear Hightlight Use This Code
richTextBox1.SelectionStart = 0;
richTextBox1.SelectAll();
richTextBox1.SelectionBackColor = Color.White;

Need to Scramble the selected Text

protected void Button_Upload_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~/Data/" + FileUpload1.FileName);
string[] readtext = File.ReadAllLines(path);
var a = readtext;
List<string> strList = new List<string>();
foreach (string s in readtext)
{
strList.Add(s);
}
ListBox1.DataSource = strList;
ListBox1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
var b = ListBox1.SelectedIndex;
var a = ListBox1.SelectedValue.ToString();
if (b < 0)
{
// no ListBox item selected;
return;
}
StringBuilder jumbleSB = new StringBuilder();
jumbleSB.Append(a);
Random rand = new Random();
int lengthSB = jumbleSB.Length;
for (int i = 0; i < lengthSB; ++i)
{
int index1 = (rand.Next() % lengthSB);
int index2 = (rand.Next() % lengthSB);
Char temp = jumbleSB[index1];
jumbleSB[index1] = jumbleSB[index2];
jumbleSB[index2] = temp;
}
Console.WriteLine(jumbleSB);
TextBox1.Text = ListBox1.Text.ToString();
//TextBox1.Text = ListBox1.Text.Insert(jumbleSB);
Here I need to Jumble the values which selected by User. When User selects a Value it has to jumble and has to come to Textbox. I am not able to displaying the Jumble values. Any help Please...??
Console.WriteLine(jumbleSB.ToString());

c# exporting sql data to csv

When exporting my data from sql to excel, it starts with the second line and not the first. I think I've narrowed down the problem to the streamwriter section of code but can't seem to work out where it's going wrong!
This is the code;
public static void ToCsv3(IDataReader myReader, string fileName, bool includeHeaderAsFirstRow)
{
const string Separator = ",";
Stream s = File.Create(fileName + ".txt");
StreamWriter streamWriter = new StreamWriter(s, Encoding.Unicode);
StringBuilder sb = null;
if (includeHeaderAsFirstRow)
{
sb = new StringBuilder();
for (int index = 0; index < myReader.FieldCount; index++)
{
if (myReader.GetName(index) != null)
sb.Append(myReader.GetName(index));
if (index < myReader.FieldCount - 1)
sb.Append(Separator);
}
streamWriter.WriteLine(sb.ToString());
}
int j = 0;
while (myReader.Read())
{
sb = new StringBuilder();
for (int index = 0; index < myReader.FieldCount - 1; index++)
{
if (!myReader.IsDBNull(index))
{
string value = myReader.GetValue(index).ToString();
if (myReader.GetFieldType(index) == typeof(String))
{
if (value.IndexOf("\"") >= 0)
value = value.Replace("\"", "\"\"");
if (value.IndexOf(Separator) >= 0)
value = "\"" + value + "\"";
}
if (j != 0)
{
if (index == 0)
{
sb.Append(Environment.NewLine);
}
}
sb.Append(value);
j = j + 1;
}
if (index < myReader.FieldCount - 1)
sb.Append(Separator);
}
if (!myReader.IsDBNull(myReader.FieldCount - 1))
sb.Append(myReader.GetValue(myReader.FieldCount).ToString().Replace(Separator, " "));
streamWriter.Write(sb.ToString());
}
myReader.Close();
streamWriter.Close();
}
I'd rather decompose the solution into creating CSV and saving it to the file:
public static IEnumerable<String> ToCsv(IDataReader reader,
Boolean includeHeaderAsFirstRow,
Char separator = ',',
Char quotation = '"') {
if (null == reader)
throw new ArgumentNullException("reader");
String qt = quotation.ToString();
StringBuilder Sb = new StringBuilder();
if (includeHeaderAsFirstRow) {
for (int i = 0; i < reader.FieldCount; ++i) {
if (i > 0)
Sb.Append(separator);
String name = reader.GetName(i);
if (name.Contains(separator) || name.Contains(quotation))
name = qt + name.Replace(qt, qt + qt) + qt;
Sb.Append(name);
}
yield return Sb.ToString();
}
while (reader.Read()) {
Sb.Clear();
for (int i = 0; i < reader.FieldCount; ++i) {
if (i > 0)
Sb.Append(separator);
if (!reader.IsDBNull(i)) {
String item = Convert.ToString(reader[i]);
if (item.Contains(separator) || item.Contains(quotation))
item = qt + item.Replace(qt, qt + qt) + qt;
Sb.Append(item);
}
}
yield return Sb.ToString();
}
}
public static void CsvToFile(String fileName,
IDataReader reader,
Char separator = ',',
Char quotation = '"') {
if (String.IsNullOrEmpty(Path.GetExtension(fileName)))
fileName += ".txt"; // ".csv" looks better here
File.WriteAllLines(fileName, ToCsv(reader, separator, quotation));
}

how to store listbox values in a string variable in c#

I want to store all listbox control values in a string with "," separated so that I can show it in the label. I'm using for loop but giving error
for (int i = 0; i < ListBox2.Items.Count; i++){
if (ListBox2.Items[i].Selected == true || ListBox2.Items.Count > 0){
string projectnames += ListBox2.Items[i].ToString();
}
}
string projectnames = "";
bool firstValue = true;
for (int i = 0; i < ListBox2.Items.Count; i++)
{
if (ListBox2.Items[i].Selected == true || ListBox2.Items.Count > 0)
{
if(!firstValue)
{
projectnames += ", " + ListBox2.Items[i].ToString();
}
else
{
projectnames += ListBox2.Items[i].ToString();
firstValue = false;
}
}
}
Instead of the loop i'd use LINQ + String.Join:
var selected = ListBox2.Items.Cast<ListItem>()
.Where(li => li.Selected)
.Select(li => li.ToString());
string projectnames = String.Join(",", selected);
On that way it's much better to read and you don't need to care about trailing commas.
This will generate a string of all selected items in the list.
string projectnames = "";
for (int i = 0; i < ListBox2.Items.Count; i++)
{
if (ListBox2.Items[i].Selected)
{
projectnames += ListBox2.Items[i].ToString() + ", ";
}
}
The most succinct method I can think of is:
var label = string.Join(",", listBox2.Items.Cast<string>());
(This uses System.Linq)
Try this, will help you!
protected void btnSave_Click(object sender, EventArgs e)
{
string selectedItem=string.Empty;
if (ListBox1.Items.Count > 0)
{
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected)
{
selectedItem += ListBox1.Items[i].Text.ToString() + ", ";
//insert command
}
}
}
}

searching presence of words in different lines in a set of lines. c# windows form

So I'm working on a text mining project and currently trying to implement info gain. I have a data in which each line depict a document. so a new line character splits different documents.
I have to generate a matrix in which columns are all the distinct words in all documents and rows are different document. each cell in this table is either 1(true) or 0(false) for if the word is present or not in that document.
Note: i have removed all repeated words in a document already.
class word_list
{
public string word;
public List<bool> doc = new List<bool>();
}
private void button2_Click(object sender, EventArgs e)
{
//Convert the string into an array of words
string[] w1 = richTextBox1.Text.Split('\n', ' ').Select(x => x.Trim().ToLower()).Distinct().ToArray();
string[] rich_doc = richTextBox1.Text.Split('\n');
List<word_list> words = new List<word_list>();
word_list temp = new word_list();
for (int i = 0; i < w1.Length; i++)
{
temp.word = w1[i];
for (int j = 0; j < rich_doc.Length; j++)//all doc array
{
List<string> doc_word = Regex.Split(rich_doc[j], #"\b").Distinct(StringComparer.CurrentCultureIgnoreCase).ToList();
for (int k = 0; k < doc_word.Count; k++)//All words in a doc
{
if (doc_word[k] == w1[i])
{
temp.doc.Add(true);
words.Add(temp);
}
else
{
temp.doc.Add(false);
words.Add(temp);
}
}
}
}
//generate matrix
int t = rich_doc.Length; //no. of docs
string final_matrix = "Doc number";
for (int i = 0; i < words[0].doc.Count; i++)
{
final_matrix += "\t" + words[i].word;
}
final_matrix += "\n";
for (int i = 0; i < t; i++) //traverse through number of docs
{
final_matrix += i + 1;
for (int h = 0; h < words.Count; h++)//traverse through each distinct word in the list
{
if (words[h].doc[t])
final_matrix += "\t1";
else
final_matrix += "\t0";
}
final_matrix += "\n";
}
richTextBox1.Text = final_matrix;
}//end of button 2
The problem is that this code is running in an infinite loop.

Categories

Resources