它比較文本中的非常簡單的場景,但我一直在比較兩個文本。在C#中我創建了一個函數splitText它將文本分割成定義的字符數,然後我將分割文本分配給一個字符串變量,然後與另一個字符串比較,但比較不起作用。c中的文本分割和文本匹配#
這裏是splitText功能
private string splitText(string Text, int startingIndex, int totalCharacters)
{
string s = "";
char[] str = new char[100];
int count = 0;
for (int i = startingIndex; i <= totalCharacters; i++)
{
str[count++] = Text[i];
}
s = new string(str);
return s;
}
這裏是我比較字符串的代碼,但它不工作
private void button1_Click(object sender, EventArgs e)
{
string s = splitText("Khuram Jan", 0, 2);
if (s.Equals("Khu"))
{
MessageBox.Show("I have done");
}
else
{
MessageBox.Show("Does not compare");
}
}
我也顯示字符串s值,它表明我Khu,但它不起作用,如果條件,alwys其他部分執行。我測試了很多,但徒勞無功解決了這個問題。
但問題是如何使用動態字符數組我是字符型[] str = new str [totalCharacters]但它給了我例外。 – 2014-09-27 10:56:00
非常感謝萊姆斯Rusano對我的幫助,現在它的工作原理, – 2014-09-27 11:04:32