#include <iostream>
#include <cstring> #include <atlcomtime.h> using namespace std; //最後一個參數返回的是子字符串的數量 CString * SplitString(CString str, char split, int& iSubStrs); int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; // initialize MFC and print and error on failur CString str = "sssdf sfds jieuri lj122 98098"; // CString str = "sfsfsf"; CString* pStr; int iSubStrs; pStr = SplitString(str, ' ', iSubStrs); //如果子字符串的數量爲1 if (iSubStrs == 1) { //Convert CString to char char* pCh = (LPSTR)(LPCTSTR)str; printf("%s\n", pCh); } else { //輸出所有子字符串 for (int i = 0; i < iSubStrs; i++) { //Convert CString to char char* pCh = (LPSTR)(LPCTSTR)pStr[i]; printf("%s\n", pCh); } delete []pStr; } system("pause"); return nRetCode; } CString * SplitString(CString str, char split, int& iSubStrs) { int iPos = 0; //分割符位置 int iNums = 0; //分割符的總數 CString strTemp = str; CString strRight; //先計算子字符串的數量 while (iPos != -1) { iPos = strTemp.Find(split); if (iPos == -1) { break; } strRight = strTemp.Mid(iPos + 1, str.GetLength()); strTemp = strRight; iNums++; } if (iNums == 0) //沒有找到分割符 { //子字符串數就是字符串本身 iSubStrs = 1; return NULL; } //子字符串數組 iSubStrs = iNums + 1; //子串的數量 = 分割符數量 + 1 CString* pStrSplit; pStrSplit = new CString[iSubStrs]; strTemp = str; CString strLeft; for (int i = 0; i < iNums; i++) { iPos = strTemp.Find(split); //左子串 strLeft = strTemp.Left(iPos); //右子串 strRight = strTemp.Mid(iPos + 1, strTemp.GetLength()); strTemp = strRight; pStrSplit[i] = strLeft; } pStrSplit[iNums] = strTemp; return pStrSplit; }
訂閱:
張貼留言 (Atom)
[SQL]顯示千分位與小數顯示
CONVERT ( data_type [ ( length ) ] , expression [ , style ] ) CONVERT style參數說明 1 (expression為 money 或 smallmoney型別): 0 : 預設,保留小數位後兩位,並四捨...
-
using System.IO; 取得程式當下檔案目錄資訊: string sPath = this .GetType().Assembly.Location; string filename = @"C:\Users\123.text&...
-
兩個函式宣告,需要 using System.Diagnostics; ----------------------使用方式------------------------------------------- OpenPress( "AAA.exe"...
-
方法1--直接獲取 //取得目前滑鼠位置 Console .WriteLine( "x:" + System.Windows.Forms. Cursor .Position.X); Console .WriteLine( "y:"...
沒有留言:
張貼留言