[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 616
  • Last Modified:

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
0
Ocrana
Asked:
Ocrana
1 Solution
 
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
 
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now