Convert CString with delimiters to int

Hi,

I have a function that gives me a CString with an "array" of values, delimiter is ",". Like example 1,2,3,4
I want now a function to read a single value. Like getint(CString aArray, int Pos). How I can do this?

Ingo
OcranaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
You can either write your own code to do that or use something that is ready and tested, e.g. http://www.codeproject.com/KB/cpp/string_tokenizer_class_in_c__.aspx ("String Tokenizer Class in C++"). Basically, the idea to tokenize a CString is (from the article):
StringTokenizer::StringTokenizer(CString str,CString sep=" ") 
{ 
    index=0; 
    count=0; 
    CString str1=""; 
    for (int i=0;i<str.GetLength() && sep.GetLength()==1;i++) 
    { 
        if(str.GetAt(i)==sep.GetAt(0)) 
        { 
            elements.Add(str1); 
            str1=""; 
        } 
        else 
        { 
            str1+=str.GetAt(i); 
        } 
    } 
    elements.Add(str1); 
    count=elements.GetSize (); 
} 
 
CString StringTokenizer::elementAt(int index) 
{ 
    if(index>=count ||index<0) 
    { 
        throw CString("Index out of Bounds"); 
    } 
    else 
        return elements.GetAt(index); 
} 
 
// use it:
 
CString str = "1,2,3,4";
StringTokenizer st(str,",");
 
for (int i = 0; i < 4; ++i) {
 
  CString sub = st.elementAt(i);
 
  int n = atoi((LPCTSTR) sub); // convert to 'int'
}

Open in new window

0
DanRollinsCommented:
Rather than implement an entire class object, here's a short routine that does the basic operation.  You may want to change it a bit since it returns -1 for an invalid index (even though -1 is a valid number for the array)


//---------------------------------------------
// Assumes input string is comma-delimited string of a series of numbers
//    e.g., "1,2,,4,567,8,,);
// nIdx is 0-based (use 0 for first element, 1 for second, etc)
// returns 0 for null entry or 0 ( i.e., ,, or ,0,)
// returns -1 if not found (for demo only: I recommend using other technique)
//
int GetValueAtIndex( CString &s, int nIdx )
{
	int nOffset=0;
	for ( int j=0; j< nIdx; j++ ) {
		nOffset= s.Find(',', nOffset ) + 1;
		if ( nOffset == 0 ) return(-1);  // end of string
	}
	return( atoi( s.Mid(nOffset) ) );
}
 
//-------------- test code
void CD39Dlg::OnButton2() 
{
	CString s="1,,345,4,5,6";
 
	int nRet= GetValueAtIndex( s, 1 ); // 0
	nRet= GetValueAtIndex( s, 2 ); // 345
	nRet= GetValueAtIndex( s, 9 ); // -1 (invalid index)
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
alb66Commented:
This is a little function that extract the first subitem:7

CString ExtractString ( CString& stringa, BOOL* pbLast, char chSep )
{
      BOOL bLast;

      CString  str;
      int pos = stringa.Find( chSep );
      if( pos != -1 )
      {
            str = stringa.Left( pos );
            stringa = stringa.Mid( pos + 1 );
            bLast = FALSE;
      }      
      else
      {
            str = stringa;
            bLast = TRUE;
      }

      if ( pbLast != NULL )
            *pbLast = bLast;

      return str;
}

You can use it in this way:

CString str = "1,2,3,4,5,6";
CWordArray wValues;
BOOL bLast = false;

while ( !bLast )
  wValues.Add( atoi( ExtractSting( str, &bLast, ',' ));



0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.