Step3:對存放讀取文件內容的string對象,進行數據提取,此處採用IndexOf與Split方法
使用軟件環境:VS2010
1、使用Split方法
strTotal爲讀取文件存放的String對象
splitString爲要分析的參數數據,用於對數據對象進行提取
Split()方法對String對象strTotal進行切割
resultLen-1,是因爲切割後,會有“1”部分是無用的,因此要減少“1”
dataCount爲textbox,用於對切割後的數據進行顯示
-----此方法無法同時對多個數據進行分析。若需要對多個數據進行分析時,需要重寫多次--------
String^ splitString = "***";
//匹配的字符串splitString,用於在Log信息中索引的數據
array<String^>^ sepMoney = gcnew array<String^>{splitString};
//Split()方法獲取所有分割後的數據,保存到數據result中
array<String^>^result = strTotal->Split(sepMoney,StringSplitOptions::None );
//獲取數組的長度,數組的長度減1即爲分割數據的選項
int resultLen = result->GetLength(0);
//分割數據,會多增加1
dataCount->Text = (resultLen-1).ToString();
2、使用IndexOf方法
totalEquip用於對所有數據的總數進行保存
sepEquip用於對需要分析的數據進行保存到數組中
nCnt爲每次從數組中讀取的數據進行統計的個數進行保存
itemCount爲textbox,用於對總統計結果進行顯示
System::Convert::ToString()用於將整數型數據轉換爲String數據進行顯示到textbox中
int totalEquip = 0;
array<String^>^ sepEquip = gcnew array<String^>{"***","&&&"};
for (int count = 0;count < sepEquip->GetLength(0);count++)
{
int nNextIdx = 0, nCnt = 0, nRetIdx = -1;
while ((nRetIdx = strTotal->IndexOf(sepEquip[count], nNextIdx)) != -1)
{
nNextIdx = nRetIdx + 1;
nCnt++;
}
//對每次分析的結果進行求和
totalEquip +=nCnt;
}
itemCount->Text = System::Convert::ToString(totalEquip);