(wstring去除空格)char字符串去除空格、wchar_t去除空格、判斷一個字符串是包含所需要的數據

公衆號:程序員崛起

這裏不寫完整的代碼,說下大概的情況

場景:

有一個字符串類型爲 wchar_t 類型,需要解析,

(1)將解析後的每個單元數據去除空格,

(2)判斷是否僅有 ‘數字’\'.'、‘空格’,刪除其中的空格;倘若不是純數字則不去除空格;

思路:

如果看了上一篇博客,大概就知道我的思路,使用 CString 容器完成這些操作;

期間遇到一個奇怪的問題,就是當自己使用 wstring 使用 erase 無法刪除(空格爲 L‘ ’ ,如果有哪位大佬知道麻煩指教),最後轉爲 CString才能刪除。

這個demo是解決第二個第一個屏蔽裏面的if判斷即可;

解析的數據格式:

@dwi oidehfeh@dk asjhj哦我的 大家 思 考@     5  @

第一種目標數據:

dwioidehfehdkasjhj哦我的大家思考5

第二種目標數據:

dwi oidehfehdk asjhj哦我的 大家 思 考5

CString csReemSpace
  void REMOVESPACE(CString doc)
{
 CString csReemSpcetemp(doc.GetString());
 csReemSpace = csReemSpcetemp;
 if (doc.GetLength() != 0)
 {
  csReemSpace.Remove(L' ');
 }
}
void mf(char *str)
{
	 int nNum = 0;
	 CString csRowInf(str);
	 wstring wsRowinf = csRowInf;
	 if (!wsRowinf.empty())
	 {
		 //  獲取數據的個數 
		 vector <int> vColumLen;
		 int VNLineNumber[256][50] = { 0 };
		 vector <wstring> vwsCellTexts;
		 vector <wstring> arryCellTexts;

		 int nDocSize = wsRowinf.find_last_of(_T('@')) + 1;
		 vColumLen.push_back(0);
		 int index = 1;
		 while (index < nDocSize)
		 {
			 //列的個數 vColumLen.size() - 1
			 if (wsRowinf.at(index) == _T('@'))
			 {
				 vColumLen.push_back(index);
			 }
			 index++;
		 }
		index = 0;
		for (index = 1; index < vColumLen.size(); index++)
		{
			// 解析後獲取數據存入 arryCellTexts
			arryCellTexts.push_back(wsRowinf.substr(vColumLen.at(index - 1) + 1, vColumLen.at(index) - vColumLen.at(index - 1) - 1).c_str());
			// 是否只包含數字,是否爲數字
			REMOVESPACE(csReemSpace.GetString());
           if (csReemSpace.SpanIncluding(L"0123456789.") == csReemSpace)
                arryCellTexts[index - 1].Remove(L' ');
		    // mapXmlReport[nNumDoc].writeTextXml(arryCellTexts[index - 1].GetString(), pSet);
		}

		//if (bWrap != 0)
		//{
			//mapXmlReport[nNumDoc].writeTextXml(_T("\n"), NULL);
		//}
		nNum = arryCellTexts.size();
		vColumLen.clear();
		vwsCellTexts.clear();
		arryCellTexts.clear();
	 }
}

這個東西讓我思考以下的結果東西:

1、算法(比如數據結構、結構體)

2、STL就是容器,容器的好多作用自己還是不甚理解

3、協議(比如,經典的TCP協議、格式的定義和解析,自己在思考什麼時候實現以下試試)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章