如何用两个逗号 , 分割字符串?
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();
}
}
}
这是分割线:
string[] splittedwords = words.Split(new string[] { ",," }, StringSplitOptions.None);
问题是如果我输入 textBox1 例如 sadsdss,s,form1,f,dd,
然后所有超过两个逗号的地方在突出显示单词时都算作空字符串:
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;
}
异常在线:
int jx = s.IndexOf(phrase, ix, StringComparison.CurrentCultureIgnoreCase);
System.ArgumentOutOfRangeException: '索引超出范围。必须为非负数且小于集合的大小。
参数名称:startIndex'
因为词组是空串""
我想做的是,每个地方都有两个以上的逗号,如 , 即使用户输入 s,1,form1,
所以单词s 1 form1 and , 都应该算作结果和应该突出显示的单词。
如果您想删除空条目,只需借助 StringSplitOptions.RemoveEmptyEntries 选项即可:
string[] splittedwords = words.Split(
new string[] { ",," },
StringSplitOptions.RemoveEmptyEntries);
另一种可能性是在 Linq 的帮助下 query,如果您想 exclude,这会很有帮助(过滤掉)一些词,例如
using System.Linq;
...
string[] splittedwords = words
.Split(new string[] { ",," }, StringSplitOptions.None)
.Where(item => !string.IsNullOrWhiteSpace(item))
.ToArray();
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();
}
}
}
这是分割线:
string[] splittedwords = words.Split(new string[] { ",," }, StringSplitOptions.None);
问题是如果我输入 textBox1 例如 sadsdss,s,form1,f,dd,
然后所有超过两个逗号的地方在突出显示单词时都算作空字符串:
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;
}
异常在线:
int jx = s.IndexOf(phrase, ix, StringComparison.CurrentCultureIgnoreCase);
System.ArgumentOutOfRangeException: '索引超出范围。必须为非负数且小于集合的大小。 参数名称:startIndex'
因为词组是空串""
我想做的是,每个地方都有两个以上的逗号,如 , 即使用户输入 s,1,form1,
所以单词s 1 form1 and , 都应该算作结果和应该突出显示的单词。
如果您想删除空条目,只需借助 StringSplitOptions.RemoveEmptyEntries 选项即可:
string[] splittedwords = words.Split(
new string[] { ",," },
StringSplitOptions.RemoveEmptyEntries);
另一种可能性是在 Linq 的帮助下 query,如果您想 exclude,这会很有帮助(过滤掉)一些词,例如
using System.Linq;
...
string[] splittedwords = words
.Split(new string[] { ",," }, StringSplitOptions.None)
.Where(item => !string.IsNullOrWhiteSpace(item))
.ToArray();