简体   繁体   English

如何用两个逗号分隔字符串,,?

[英]How can I split strings by two commas ,,?

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,,,,,,问题是如果我输入 textBox1 例如 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. System.ArgumentOutOfRangeException: '索引超出范围。 Must be non-negative and less than the size of the collection.必须是非负数且小于集合的大小。 Parameter name: startIndex'参数名称: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,,,,,,我想要做的是每个地方都有两个以上的逗号,例如,,即使用户键入 s,,1,,form1,,,,,, 也将其视为字符串作为单词

so the words s 1 form1 and,,,,,, all should be counted as results and words that should be highlighted.所以单词 s 1 form1 和,,,,,, all 应该算作结果和应该突出显示的单词。

If you want to remove empty entries, just do it with a help of StringSplitOptions.RemoveEmptyEntries option:如果要删除空条目,只需借助StringSplitOptions.RemoveEmptyEntries选项即可:

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, eg另一种可能性是在Linq的帮助下进行查询,如果您想排除(过滤掉)一些单词,这可能会有所帮助,例如

using System.Linq;

...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM