Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,
I want to extract part of a text I get from list box.
Here is the scenario:

int indexValue = m_listItem.GetCursel();
CString strText;
m_listItem.GetText(indexValue, strText);


I only put that out so you can understand where am coming from.
Alright, assuming the text in the strText is: "1 17:28:07 76 172.17.70.10 62996 193.65.41.23 53"
My problem is how could I extract just 17:28:07 or 172.17.70.10 from the text.

Many thanks.
Posted
Updated 21-Nov-10 6:58am
v2
Comments
Manfred Rudolf Bihy 21-Nov-10 12:59pm    
Cleaned up the code block and some typos.

You could use regular expressions to do the extraction. Maybe just splitting the string at a space character and grabbing the relevant parts would suffice. That really depends on how your input is structured.

Cheers

Manfred
 
Share this answer
 
Comments
Adeji 21-Nov-10 13:14pm    
Thanks for your response.

But, how do i do the splitting?
Manfred Rudolf Bihy 21-Nov-10 13:18pm    
Look at the Tokenize bit CPallini mentioned in his answer. That should do the trick.
Adeji 21-Nov-10 13:25pm    
Thanks, will have a go on that.
You may (from highest level downwards...):
  • (As already suggest) Use a pattern matching library, like a regular expression parser (google for a free C++ library).
  • Use simple CString methods like Tokenize[^].
  • Parse yourself the string for such patterns (you would have some fun with this approach...).
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 21-Nov-10 13:19pm    
If it's a simply structured string I'll second the string tokenize approach. My 5!
BTW: Does one still use str_tok like way back in C (the C without any decorations on it ;) )
Adeji 21-Nov-10 13:25pm    
Cheers.
CPallini 23-Nov-10 7:40am    
No one use str_tok nowdays. Someone, like me, uses strtok, however... :rolleyes:
Hi,

CString::Tokenize() requires to be iterated until failure to extract all the parts in your input.

With the help of the Standard C++ Library you can do it at once:
C++
#include <vector>
#include <sstream>
#include <iterator>
#include <algorithm>
// ...
int indexValue = m_listItem.GetCursel();
CString strText;
m_listItem.GetText(indexValue, strText);

std::vector<std::string> parts;
std::istringstream iss(strText.GetBuffer());
std::copy(std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>(), std::back_inserter(parts));
With "1 17:28:07 76 172.17.70.10 62996 193.65.41.23 53" in strText, you get in parts[0x00000007] ("1","17:28:07","76","172.17.70.10","62996","193.65.41.23","53").

cheers,
AR
 
Share this answer
 
Another, simpler idea, is to create an extracter function that converts the string into an array of zstrings, then simply extracts the requested substring by index. EX:


BOOL GetSubStringByIndex(int iIndexValue, int iSubstring, LPTSTR cpRetval)
{
  TCHAR caBuf[512];
  *cpRetval = 0;
  int iOffset = 0;

  m_listItem.GetText(indexValue,caBuf);

  caBuf[_tcslen(caBuf)+1] = 0; // double null terminate.
  LPTSTR cpSeg = caBuf;
  while (*cpSeg)
    {
      if (*cpSeg == ' ')
        *cpSeg = 0;
      cpSeg++;
    }

  // now, you can loop through them
  cpSeg = caBuf;
  while (*cpSeg)
    {
      if (iOffset == iSubstring)
        {
          _tcscpy(cpRetval,cpSeg);
          return TRUE;
        }

      cpSeg += _tcslen(cpSeg)+1;
      iOffset++;
    }

  return FALSE;
}
 
Share this answer
 
v2
Comments
Alain Rist 23-Nov-10 9:49am    
Simpler indeed :) and safer as well :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900