Files
Text-Analyzer/Metoder/Form1.cs
2026-01-19 18:42:20 +01:00

170 lines
4.6 KiB
C#

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
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// button binds
private void btn_find_Click(object sender, EventArgs e)
{
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)
{
lbl_found.Visible = false;
}
private void btn_sort_Click(object sender, EventArgs e)
{
SortWords();
}
// methods
void UpdateFindResult(int foundCount)
{
lbl_found.Visible = true;
string suffix;
// correct grammar
if (foundCount == 1)
suffix = " item found";
else
suffix = " items found";
lbl_found.Text = foundCount.ToString() + suffix;
}
Dictionary<int, int> GetMatchesIndices(string pattern, string text)
{
Dictionary<int, int> dict = new Dictionary<int, int>();
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 pattern)
{
RichTextBox richTextBox = txtbx_text;
string text = richTextBox.Text;
// reset old highlight
richTextBox.SelectAll();
richTextBox.SelectionBackColor = richTextBox.BackColor;
int found = 0;
int caret = richTextBox.SelectionStart; // set cursor to start
foreach (KeyValuePair<int, int> occurance in GetMatchesIndices(pattern, text))
{
richTextBox.Select(occurance.Key, occurance.Value); // select pattern occurance
richTextBox.SelectionBackColor = Color.LightBlue; // highlight
found++;
}
// reset cursor
richTextBox.Select(caret, 0);
// update find label
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); // split by spaces
return words.ToList();
}
}
}