2014年11月10日 星期一

C++ String How to cut by a character and How to reverse cut ? examples (字串切割 正向(我將整個字串存入到另一字串直到遇到某個字符就停止),反向切割( 真實範例,當傳入檔案位置,我只要資料夾位置時) )




#include <iostream>
#include <string>

int main()
{
std::string bai = "C:\\Users\\pjtseng\\Desktop\\tests.shp"; //file location
std::string newBai = "";
int x;
for ( x = bai.length() - 1; x >= 0;x--)                                  //reverse string array numbers
{
if (bai[x] == '\\')                                                          //When I find \ ,break for loop
{
break;
}
}
newBai = bai.substr(0, x);                                                  // x = break number
std::cout << newBai;                                                          //You can see the true

return 0 ;
}




int main()
{
std::string originalSRID = "GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137,298.257223563]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]]";

std::string newSRID = "";
for (auto& x : originalSRID)                                             //I only need before "first , "
{
if (x == ','){ break; }
else         { newSRID += x;   }
}

std::cout << newSRID;
return 0 ;
}

沒有留言:

張貼留言