• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1496
  • Last Modified:

How can I split a string by a string delimeter in MFC?

Hi all,

I have the need to split a string by a specified string, ie...

   Hello<->Everyone<-><->How<->Are You

based on the delimeter '<->' needs to produce the following array:

   a[0] = 'Hello'
   a[1] = 'Everyone'
   a[2] = ''
   a[3] = How
   a[4] = Are You

Can anyone help?

Thanks,

James
0
James Atkin
Asked:
James Atkin
  • 3
  • 3
  • 2
  • +1
1 Solution
 
janjerellCommented:
HI Try this:


string s2 = "Hello<->Everyone<-><->How<->Are You";
string[] a = s2.Split(new string[] { "<->" },StringSplitOptions.None);

Open in new window

0
 
alb66Commented:
MFC has no specific methods.
You can use strtok():
http://msdn.microsoft.com/en-us/library/2c8d19sb(VS.71).aspx
0
 
James AtkinSenior Principle Software EngineerAuthor Commented:
Sorry, this is an MFC issue - sadly the above is not possible :-(
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
James AtkinSenior Principle Software EngineerAuthor Commented:
the above response was in response to janjerell's comment.

alb66, the problem i have with the strtok functions is that they ignore multiple delimeters and do not return blank entries as above...
0
 
alb66Commented:
Yes yor are right...
You need to write your own function:
CString CUtility::ExtractString ( CString& stringa, BOOL* pbLast, const CString& strSepa )
{
	BOOL bLast;
 
	CString  str;
	int pos = stringa.Find( strSepa );
	if( pos != -1 )
	{
		str = stringa.Left( pos );
		stringa = stringa.Mid( pos + strSepa.GetLength() );
		bLast = FALSE;
	}      
	else
	{
		str = stringa;
		bLast = TRUE;
	}
 
	if ( pbLast != NULL )
		*pbLast = bLast;
 
	return str;
}
 
 
 
CString strTest( "Hello<->Everyone<-><->How<->Are You" );
CStringArray vstr;
 
BOOL bLast = !strTest.IsEmpty();
while ( !bLast )
  vstr.Add( CUtility::ExtractString ( strTest, &bLast, "<->" ));

Open in new window

0
 
ZoppoCommented:
Hi jatkin,

attached you can find a function which does it. Just call it like this:

> CString strText = "Hello<->Everyone<-><->How<->Are You";
> CStringArray tokens;
> ParseTokens( strText, "<->", tokens );

Hope that helps,

ZOPPO
void
ParseTokens( CString strString, CString strDelimiter, CStringArray& tokens )
{
	int iNum = 0;
 
	int iCurrPos= 0;
 
	while( -1 != ( iCurrPos = strString.Find( strDelimiter ) ) )
	{
		tokens.Add( strString.Left( iCurrPos ) );
 
		strString = strString.Right( strString.GetLength() - iCurrPos - strDelimiter.GetLength() );
	}
 
	tokens.Add( strString );
}

Open in new window

0
 
janjerellCommented:
oh sorry.. didn't notice it was MFC...
0
 
ZoppoCommented:
ehm sorry, the function has a little failure - the first line 'int iNum' is not needed, you can delete it ...
0
 
James AtkinSenior Principle Software EngineerAuthor Commented:
Perfect, thanks!!!
0
 
ZoppoCommented:
You're welcome - I'm glad I could help.

Have a nice day,

best regards,

ZOPPO
0

Featured Post

Technology Partners: 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!

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now