String processing?
I have this sample
std::string
:
Now, I have the position of the
in
...
I need to be able to process the string between the slashes after it.
So I need to have this output of my algorithm :
Note that the content could be
only or something...
1 2 3 4 5 6 7 8
|
size_t F_position;
size_t next_slash = path.find('/', F_position);
if (next_slash == path.npos || next_slash + 1 == path.size())
//invalid path?
size_t next_next_slash = path.find('/', next_slash);
if (next_next_slash == path.npos)
//invalid path?
std::string version(path.begin() + next_slash + 1, path.begin() + next_next_slash);
|
Although I think this is a great application of regexes.
Thanks! I also found my own solution just after posting:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
#include <fstream>
#include <string>
#include <iostream>
#include <cstring>
using namespace std;
string get(string file_name)
{
ifstream OpenFile(file_name.c_str());
char ch;
string file_content;
while(!OpenFile.eof())
{
OpenFile.get(ch);
file_content += ch;
}
OpenFile.close();
return file_content;
}
bool contains(string string_to_find, string parent_string)
{
if (parent_string.find(string_to_find) != string::npos)
{
return true;
}
else
{
return false;
}
}
int main()
{
string ver;
string pro = "dhhhhhhhhFT/5.3.4/dgsj";
if (contains("FT/", pro))
{
int pos_1 = pro.find("FT/") + 3;
char ch;
while (ch != '/')
{
if (pro[pos_1] == '/')
{
break;
}
ch = pro[pos_1];
ver += ch;
pos_1++;
}
}
cout << ver;
return 0;
}
|
You can kind of skip the
contains() function altogether.
1 2 3 4 5 6
|
size_t pos_1 = pro.find("FT/");
if (pos_1 != pro.npos)
{
pos_1 += 3;
...
}
|
You can also use
string::find() again to find the next '/', and
string::substr() to get out the substring.
Hope this helps.
Topic archived. No new replies allowed.