[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2009-05-08
10
Medium Priority
?
1,469 Views
Last Modified: 2013-11-20
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
Comment
Question by:James Atkin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 3

Expert Comment

by:janjerell
ID: 24334232
HI Try this:


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

Open in new window

0
 
LVL 19

Expert Comment

by:alb66
ID: 24334249
MFC has no specific methods.
You can use strtok():
http://msdn.microsoft.com/en-us/library/2c8d19sb(VS.71).aspx
0
 
LVL 4

Author Comment

by:James Atkin
ID: 24334250
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.

 
LVL 4

Author Comment

by:James Atkin
ID: 24334269
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
 
LVL 19

Expert Comment

by:alb66
ID: 24334292
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
 
LVL 31

Accepted Solution

by:
Zoppo earned 2000 total points
ID: 24334294
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
 
LVL 3

Expert Comment

by:janjerell
ID: 24334296
oh sorry.. didn't notice it was MFC...
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 24334307
ehm sorry, the function has a little failure - the first line 'int iNum' is not needed, you can delete it ...
0
 
LVL 4

Author Closing Comment

by:James Atkin
ID: 31579354
Perfect, thanks!!!
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 24334457
You're welcome - I'm glad I could help.

Have a nice day,

best regards,

ZOPPO
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

650 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