By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,588 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

how to find nth quote char?

P: n/a
hi , i have a string, looks like
~~ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") =
"FileSystemTest", "FileSystemTest_2003.vcproj",
"{ABC6E827-F34C-4E45-B384-AC33DF83803B}" ~~

what i want get is between 5th and 6th doule quote char, i.e.
FileSystemTest_2003.vcproj

is there any general algorithm to complete this simple task in simple
way.

my implementation is :

static const TCHAR quoteChar = '"';
static const TCHAR equalChar = '=';
size_t firstposEqual = data.find(equalChar);

size_t firstposQuote = data.find(quoteChar,++firstposEqual);
if (firstposQuote == string::npos)
return;

size_t secondQuoteChar = data.find(quoteChar,++firstposQuote);
if (secondQuoteChar == string::npos)
return;
//relative path
firstposQuote = data.find(quoteChar,++secondQuoteChar);
if (firstposQuote == string::npos)
return;
secondQuoteChar = data.find(quoteChar,++firstposQuote);
if (secondQuoteChar == string::npos)
return;
string relativePath =
data.substr(firstposQuote,secondQuoteChar-firstposQuote);

i think it is very awful!!

thanks

Dec 14 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"baibaichen" <ba********@gmail.com> schrieb im Newsbeitrag
news:11**********************@g47g2000cwa.googlegr oups.com...
hi , i have a string, looks like
~~ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") =
"FileSystemTest", "FileSystemTest_2003.vcproj",
"{ABC6E827-F34C-4E45-B384-AC33DF83803B}" ~~

what i want get is between 5th and 6th doule quote char, i.e.
FileSystemTest_2003.vcproj

is there any general algorithm to complete this simple task in
simple
way.


I would run a loop to count 6x "" and then store the positions of 5th
and 6th quotes...
Dec 14 '05 #2

P: n/a
yes, you are right, but it is just impovement....., what i want to get
is looks like:

pair<iterator, iterator> pair_find(iterator, iterator, value )

Dec 14 '05 #3

P: n/a
baibaichen wrote:
yes, you are right, but it is just impovement....., what i want to get
is looks like:

pair<iterator, iterator> pair_find(iterator, iterator, value )


Is this the sort of thing you are trying to write?

/* Find the n'th and n+1'th occurrence of value between a and b.
First result is iterator to element after n'th occurrence.
Second result is iterator to element before n+1'th occurrence.
If reached end of given range, throws an exception
*/
template<typename iter, typename valtype>
pair<iter, iter>
pair_find(
iter a,
iter b,
const valtype& value,
size_t n)
{
pair<iter, iter> result;
iter j = a;
for(size_t i = 0; i < n; ++i)
{
while(j != b && *j != value) ++j;
if(j == b) throw i;
++j;
}
result.first = j;
while(j != b && *j != value) ++j;
if(j == b) throw size_t(-1);
result.second = j;
return result;
}

I'm quite a beginner at C++ myself. If there are problems with this code
can someone please help me correct them?

--
Simon.
Dec 14 '05 #4

P: n/a
On 2005-12-14, baibaichen <ba********@gmail.com> wrote:
hi , i have a string, looks like
~~ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") =
"FileSystemTest", "FileSystemTest_2003.vcproj",
"{ABC6E827-F34C-4E45-B384-AC33DF83803B}" ~~

what i want get is between 5th and 6th doule quote char, i.e.
FileSystemTest_2003.vcproj

is there any general algorithm to complete this simple task in
simple way.


A finite state machine that found quoted strings would be of
help, but you'd have to write it yourself.

You might want to take a peek at boost::tokenizer or
boost::regex, either of which could provide help.

Here's a boost::tokenizer sample solution.

#include <iostream>
#include <string>
#include <boost/tokenizer.hpp>

int main()
{

std::string line = "Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\")"
" = \"FileSystemTest\", \"FileSystemTest_2003.vcproj\", \"{ABC6E827-"
"F34C-4E45-B384-AC33DF83803B}\"";
typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
tokenizer tokens(line, boost::char_separator<char>("\""));
int i = 0;
std::string proj_file = "\"Not Found\"";
for (tokenizer::iterator iter = tokens.begin(); iter != tokens.end()
; ++iter, ++i)
{
if (i == 5) {
proj_file = *iter;
break;
}
}
std::cout << "[" << proj_file << "]\n";
return 0;
}

--
Neil Cerutti
Dec 14 '05 #5

P: n/a
On Tue, 13 Dec 2005 21:03:40 -0800, baibaichen wrote:
hi , i have a string, looks like
~~ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") =
"FileSystemTest", "FileSystemTest_2003.vcproj",
"{ABC6E827-F34C-4E45-B384-AC33DF83803B}" ~~

what i want get is between 5th and 6th doule quote char, i.e.
FileSystemTest_2003.vcproj

is there any general algorithm to complete this simple task in simple
way.


One way:

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

// Create a predicate for find_if.
struct NFindPred
{
NFindPred(char c, int which) : m_c(c), m_remaining(which) {}

bool operator ()(char c)
{ return c == m_c && --m_remaining == 0; }

private:
char m_c;
int m_remaining;
};

int main()
{
// string (broken across lines, but still single string)
string s =
"~~ Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") ="
"\"FileSystemTest\","
"\"FileSystemTest_2003.vcproj\","
\"{ABC6E827-F34C-4E45-B384-AC33DF83803B}\""
"~~";

string::const_iterator i5 =
std::find_if(s.begin(), s.end(), NFindPred('"', 5));

if (i5 != s.end())
{
string::const_iterator i6 =
find_if(s.begin(), s.end(), NFindPred('"', 6));

string subs(i5+1, i6);

cout << subs << endl;
}

return 0;
}

- Jay
Dec 14 '05 #6

P: n/a
On Wed, 14 Dec 2005 17:19:06 +0000, Jay Nabonne wrote:
On Tue, 13 Dec 2005 21:03:40 -0800, baibaichen wrote:

Slight improvements:

One way:

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

// Create a predicate for find_if.
struct NFindPred
{ // default to 1
NFindPred(char c, int which=1) : m_c(c), m_remaining(which) {}
bool operator ()(char c)
{ return c == m_c && --m_remaining == 0; }

private:
char m_c;
int m_remaining;
};

<snip>
// continue searching from here...
string::const_iterator i6 =
find_if(i5+1, s.end(), NFindPred('"'));
string subs(i5+1, i6);

cout << subs << endl;
}


- Jay

Dec 14 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.