Home > database >  How can I split strings by two commas ,,?
How can I split strings by two commas ,,?

Time:01-01

void lvnf_SelectedIndexChanged(object sender, EventArgs e)
        {
            results = new List<int>();
            richTextBox1.Text = File.ReadAllText(listViewCostumControl1.lvnf.Items[listViewCostumControl1.lvnf.SelectedIndices[0]].Text);
            FileInfo fi = new FileInfo(listViewCostumControl1.lvnf.Items[listViewCostumControl1.lvnf.SelectedIndices[0]].Text);
            lblfilesizeselected.Text = ExtensionMethods.ToFileSize(fi.Length);
            lblfilesizeselected.Visible = true;
            filePath = Path.GetDirectoryName(fi.FullName);
            string words = textBox1.Text;
            string[] splittedwords = words.Split(new string[] { ",," }, StringSplitOptions.None);
            foreach (string myword in splittedwords)
            {
                HighlightPhrase(richTextBox1, myword, Color.Yellow);
                lblviewerselectedfile.Text = results.Count.ToString();
                lblviewerselectedfile.Visible = true;
                if (results.Count > 0)
                {
                    numericUpDown1.Maximum = results.Count;
                    numericUpDown1.Enabled = true;
                    richTextBox1.SelectionStart = results[(int)numericUpDown1.Value - 1];
                    richTextBox1.ScrollToCaret();
                }
            }
        }

This is the line that make the split :

string[] splittedwords = words.Split(new string[] { ",," }, StringSplitOptions.None);

The problem is if I'm typing the textBox1 for example sadsdss,,s,,form1,,,,,,,,f,,dd,,,,,,

Then all the places that have more then two commas it count as empty string when highlighting the words :

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;
                }
                else
                {
                    box.SelectionStart = jx;
                    box.SelectionLength = phrase.Length;
                    box.SelectionColor = color;
                    ix = jx   1;
                    results.Add(jx);
                }
            }
            box.SelectionStart = pos;
            box.SelectionLength = 0;
        }

The exception is on the line :

int jx = s.IndexOf(phrase, ix, StringComparison.CurrentCultureIgnoreCase);

System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: startIndex'

because the phrase is empty string ""

what I want to do is that every place there are more then two commas like ,,, count it as string as word even if the user type s,,1,,form1,,,,,,

so the words s 1 form1 and ,,,,,, all should be counted as results and words that should be highlighted.

CodePudding user response:

If you want to remove empty entries, just do it with a help of StringSplitOptions.RemoveEmptyEntries option:

string[] splittedwords = words.Split(
  new string[] { ",," }, 
  StringSplitOptions.RemoveEmptyEntries);

Another posibility is to query with a help of Linq, which can be helpful if you want to exclude (filter out) some words, e.g.

using System.Linq;

...

string[] splittedwords = words
  .Split(new string[] { ",," }, StringSplitOptions.None)
  .Where(item => !string.IsNullOrWhiteSpace(item))
  .ToArray();
  • Related