added sort and replace
This commit is contained in:
124
Metoder/Form1.cs
124
Metoder/Form1.cs
@@ -1,6 +1,9 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Net.Security;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
using static System.Net.Mime.MediaTypeNames;
|
||||
|
||||
namespace Metoder
|
||||
@@ -12,10 +15,19 @@ namespace Metoder
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
// button binds
|
||||
private void btn_find_Click(object sender, EventArgs e)
|
||||
{
|
||||
string searchWord = txtbx_find.Text;
|
||||
Highlight(searchWord);
|
||||
string pattern = txtbx_find.Text;
|
||||
Highlight(pattern);
|
||||
}
|
||||
|
||||
private void btn_replace_Click(object sender, EventArgs e)
|
||||
{
|
||||
string pattern = txtbx_find.Text;
|
||||
string replacement = txtbx_replace.Text;
|
||||
|
||||
ReplaceFound(pattern, replacement);
|
||||
}
|
||||
|
||||
private void btn_clear_Click(object sender, EventArgs e)
|
||||
@@ -23,6 +35,13 @@ namespace Metoder
|
||||
lbl_found.Visible = false;
|
||||
}
|
||||
|
||||
private void btn_sort_Click(object sender, EventArgs e)
|
||||
{
|
||||
SortWords();
|
||||
}
|
||||
|
||||
|
||||
// methods
|
||||
void UpdateFindResult(int foundCount)
|
||||
{
|
||||
lbl_found.Visible = true;
|
||||
@@ -37,18 +56,26 @@ namespace Metoder
|
||||
lbl_found.Text = foundCount.ToString() + suffix;
|
||||
}
|
||||
|
||||
(int start, int length) FindTextIndex(string text, int startIndex)
|
||||
Dictionary<int, int> GetMatchesIndices(string pattern, string text)
|
||||
{
|
||||
int start = (text.IndexOf(text, startIndex, StringComparison.OrdinalIgnoreCase));
|
||||
if (start == -1) return (-1, 0);
|
||||
Dictionary<int, int> dict = new Dictionary<int, int>();
|
||||
|
||||
return (start, text.Length);
|
||||
if (pattern == "") return dict; // avoid errors, infinite loop
|
||||
|
||||
int index = 0;
|
||||
|
||||
// find and save start index and length of each occurance
|
||||
while ((index = text.IndexOf(pattern, index, StringComparison.OrdinalIgnoreCase)) != -1)
|
||||
{
|
||||
dict[index] = pattern.Length;
|
||||
index += pattern.Length;
|
||||
}
|
||||
|
||||
return dict;
|
||||
}
|
||||
|
||||
void Highlight(string word)
|
||||
void Highlight(string pattern)
|
||||
{
|
||||
if (word == "") return; // avoid errors, infinite loop
|
||||
|
||||
RichTextBox richTextBox = txtbx_text;
|
||||
string text = richTextBox.Text;
|
||||
|
||||
@@ -57,17 +84,13 @@ namespace Metoder
|
||||
richTextBox.SelectionBackColor = richTextBox.BackColor;
|
||||
|
||||
int found = 0;
|
||||
int caret = richTextBox.SelectionStart; // set cursor
|
||||
int caret = richTextBox.SelectionStart; // set cursor to start
|
||||
|
||||
var words = GetWords();
|
||||
int index = 0;
|
||||
|
||||
while ((index = text.IndexOf(word, index, StringComparison.OrdinalIgnoreCase)) != -1)
|
||||
foreach (KeyValuePair<int, int> occurance in GetMatchesIndices(pattern, text))
|
||||
{
|
||||
richTextBox.Select(index, word.Length); // select word
|
||||
richTextBox.SelectionBackColor = Color.LightBlue;
|
||||
found++; // count found
|
||||
index = index + word.Length; // offset (ignore word next loop)
|
||||
richTextBox.Select(occurance.Key, occurance.Value); // select pattern occurance
|
||||
richTextBox.SelectionBackColor = Color.LightBlue; // highlight
|
||||
found++;
|
||||
}
|
||||
|
||||
// reset cursor
|
||||
@@ -77,17 +100,70 @@ namespace Metoder
|
||||
UpdateFindResult(found);
|
||||
}
|
||||
|
||||
void ReplaceFound(string pattern, string replacement)
|
||||
{
|
||||
string text = txtbx_text.Text;
|
||||
|
||||
Dictionary<int, int> matches = GetMatchesIndices(pattern, text);
|
||||
|
||||
if (matches.Count == 0) return;
|
||||
|
||||
string finalString = ""; // or string.Empty
|
||||
int currentIndex = 0;
|
||||
|
||||
foreach (KeyValuePair<int, int> occurrence in matches)
|
||||
{
|
||||
// add text before occurrence
|
||||
if (occurrence.Key > currentIndex)
|
||||
{
|
||||
finalString += text.Substring(currentIndex, occurrence.Key - currentIndex);
|
||||
}
|
||||
|
||||
// add replacement to result
|
||||
finalString += replacement;
|
||||
|
||||
// move past occurrence
|
||||
currentIndex = occurrence.Key + occurrence.Value;
|
||||
}
|
||||
|
||||
// add remaining text
|
||||
if (currentIndex < text.Length)
|
||||
{
|
||||
finalString += text.Substring(currentIndex);
|
||||
}
|
||||
|
||||
txtbx_text.Text = finalString;
|
||||
}
|
||||
|
||||
void SortWords()
|
||||
{
|
||||
List<string> words = GetSortedWords(GetWords());
|
||||
|
||||
string finalString = "";
|
||||
|
||||
// add each sorted word separated by spaces
|
||||
foreach (string word in words)
|
||||
{
|
||||
finalString += word;
|
||||
finalString += " ";
|
||||
}
|
||||
|
||||
txtbx_text.Text = finalString;
|
||||
}
|
||||
|
||||
List<string> GetSortedWords(List<string> words)
|
||||
{
|
||||
words.Sort();
|
||||
|
||||
return words;
|
||||
}
|
||||
|
||||
List<string> GetWords() // get list of words separated by spaces
|
||||
{
|
||||
string text = txtbx_text.Text;
|
||||
string[] words = text.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
|
||||
string[] words = text.Split((char[])null, StringSplitOptions.RemoveEmptyEntries); // split by spaces
|
||||
|
||||
return words.ToList();
|
||||
}
|
||||
|
||||
private void btn_replace_Click(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user