[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Extract specfic tokens from a CString

Posted on 2005-04-26
7
Medium Priority
?
576 Views
Last Modified: 2013-11-20
Hi,

I have a CString which likes "{Projectname field} is <Projectname value> ... {[[Editor]]} ..."

There are 3 cases for a token:
1. inside "{}";
2. inside "<>";
3. inside "{[[]]}".

I want to extract them all and replace them with the actual value.

Sample codes will be greatly appreciated.

Thanks!
0
Comment
Question by:xxg4813
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 13870017
You could use
void ReplaceInBetween ( CString& rstrLine, CString& rstrSubst, LPCTSTR pszStart, LPCTSTR pszEnd) {

    int nStart = rstrLine.Find(pszStart);

    if ( -1 == nStart) return;

    int nEnd = rstrLine.Find(pszStart);

    if ( -1 == nStart) return;

    CString strFront = rstrLine.Left(nStart - 1);
    CString strBack = rstrLine.Left(nEnd + lstrlen(pszEnd) + 1);

    rstrLine = strFront + rstrSubst + strBack;
}

and call it like

CString str = "{Projectname field} is <Projectname value> ... {[[Editor]]} ...";
CString strSubst = "Some real Value";

ReplaceInBetween(str,strSubst,"{[[","]]}");
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13870050
if string really contains literally  "{Projectname field} is <Projectname value> ... {[[Editor]]} ..."
then you can use CString::Replace() function, something like

CString str = "{Projectname field} is <Projectname value> ... {[[Editor]]} ...";

str.Replace("{Projectname field}", theRealProjName);

....etcetera...
0
 
LVL 4

Author Comment

by:xxg4813
ID: 13870590
Jkr,
I see your point but my problem is that I need to replace 3 caess in a same traveral of the string, i.e., need to extrace content between "{}", "<>", and "{[[]]}" at the same time. Could you revise your code a little bit to handle the 3 cases at the same time?

Jaime,
The content between can be changed with a lot of choices. It's not fixed.



0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:jkr
ID: 13870613
>>I need to replace 3 caess in a same traveral of the string

That can't be done at once - what about

ReplaceInBetween(str,strSubst,"{[[","]]}");
ReplaceInBetween(str,strSubst,"{","}");
ReplaceInBetween(str,strSubst,"[","]");

?

BTW, a lil' correction:

void ReplaceInBetween ( CString& rstrLine, CString& rstrSubst, LPCTSTR pszStart, LPCTSTR pszEnd) {

   int nStart = rstrLine.Find(pszStart);

   if ( -1 == nStart) return;

   int nEnd = rstrLine.Find(pszEnd);

   if ( -1 == nStart) return;

   CString strFront = rstrLine.Left(nStart - 1);
   CString strBack = rstrLine.Left(nEnd + lstrlen(pszEnd) + 1);

   rstrLine = strFront + rstrSubst + strBack;
}
0
 
LVL 4

Author Comment

by:xxg4813
ID: 13871121
JKR,

To traverse 3 times the whole CString is unacceptable. And your code can only replace one instance for each case.

Thanks.
0
 
LVL 45

Accepted Solution

by:
AndyAinscow earned 1500 total points
ID: 13873532
To traverse 3 times the whole CString is unacceptable.

Roughly.
char ch;
for(int i = 0;i<str.GetLength(); i++)
{
  ch=str[i];
  if(ch=='[')
  {
    //possible token here
  }
  else if(ch=='<')
  {
  //another possible token
  }
}


I think you get the idea (this only traverses the string once)
0
 
LVL 86

Expert Comment

by:jkr
ID: 13875933
Thank you xxg4813 - lloking foward to hekp you again in the future - I think you get the idea...
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Loops Section Overview
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question