using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TJ
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public int Pd(string s)
{
int len = s.Length;
//s=str.Substring(str.Length-i); // or str=str.Remove(0,str.Length-i); k = k.Substring(k.Length-1, 1);
// string str = s.Substring(s.Length -1,1);
//s = s.Remove(s.LastIndexOf(","), 1);
/* if (str==","||str==".")
{
s = s.Remove(s.Length - 2,s.Length -1);
}*/
ListViewItem li = new ListViewItem();
for (int i = 0; i < listView1.Items.Count; i++)
{
if (listView1.Items[i].SubItems[0].Text == s)
{
return 0;
}
}
return 1;
}
public int Pdc(string[] sj,string s)
{
int tem=0;
for (int i = 0; i < sj.Length; i++)
{
if (s == sj[i])
tem++;
}
return tem;
}
private void button1_Click(object sender, EventArgs e)
{
string center = txtDate.Text;
string str = center.ToLower();
string[] parm = str.Split(' ');
int len = parm.Length;
this.listView1.View = View.Details;
this.listView1.GridLines = true;
this.listView1.HeaderStyle = ColumnHeaderStyle.Nonclickable;
this.listView1.Columns.Add("詞匯", 120, HorizontalAlignment.Right);
this.listView1.Columns.Add("出現次數", 120, HorizontalAlignment.Right);
this.listView1.Visible = true;
for (int i = 0; i < len; i++)
{
string s = parm[i];
s = s.Remove(s.LastIndexOf(","), 1);
int tem = Pd(parm[i]);
if (tem == 1 && parm[i] != "")
{
int m = Pdc(parm, parm[i]);
string mm = Convert.ToString(m);
ListViewItem li = new ListViewItem();
li.SubItems[0].Text = parm[i].ToString();
li.SubItems.Add(mm);
this.listView1.Items.Add(li);
}
}
}
}
}
你好,我覺得問題應該出在這裡:
string s = parm[i];
s = s.Remove(s.LastIndexOf(","), 1);
首先,s.LastIndexOf(string),會返回string在s中首次出現的位置,若未出現,則返回-1,s.Remove(StartIndex, 1)會在s的StartIndxe位置開始刪除1個字符。
IDE報的錯誤是"StartIndex不能小於0",所以錯誤原因應該是parm[i]中不包含",",建議樓主檢查一下parm[]中的數據。