Split string (CString version) _null

Source: Internet
Author: User

To split a string based on multiple delimiters

SOURCE string

Seprator Separator

Number of substrings after count split

Return value: Split substring

cstring* Extractstr (LPCTSTR source, LPCTSTR seprator, int *count)

{
	int		isubstringcount,nindex=0;
	CString strsource=source,strseperator=seprator,*pstrsubstrings;

	if (Strseperator.getlength () ==0)
	{
		*count=0;
		return NULL;
	}

	All the delimiters in the seprator are replaced by the first one, and the number of seprator in the source is counted
	isubstringcount =1;

	for (int i=0;i<strsource.getlength (); i++)
	{
		if (strsource[i]==strseperator[0))
			isubstringcount+ +;
	}

	for (int i=1;i<strseperator.getlength (); i++)
	{
		isubstringcount+=strsource.replace (strseperator[i), Strseperator[0]);
	}

	Pstrsubstrings=new Cstring[isubstringcount];

	int nnewindex,ncount=0;
	while (Nindex<strsource.getlength ())
	{
		nnewindex=strsource.find (strseperator[0],nindex);
		if (nnewindex>=0)
			pstrsubstrings[ncount++]=strsource.mid (nindex,nnewindex-nindex);
		else
			Pstrsubstrings[ncount++]=strsource.mid (nindex);

		NINDEX+=PSTRSUBSTRINGS[NCOUNT-1]. GetLength () +1;
	}

	*count=isubstringcount;
	return pstrsubstrings;
}

Free memory

void Freestrings (CString *lpstr, int count)
{
	if (Lpstr==null | | count<=0)
	{return
		;
	}

	Delete[] LPSTR
}

Example:

CString strsample=text ("Aa,bb,cc,dd");
int ncount=0;
CString Psubstrings=extractstr (Strsample,text (","), &ncount);
....
Freestrings (Psubstrings,ncount)///free memory after using substring



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.