C + + finds the number of commas in a string, and gets a comma-separated number (set here as a string)

Source: Internet
Author: User

Since the read string is separated by commas, each value separated by a comma is now stored as a string, as follows: 1. Find the number of commas in a string:

Find the number of ', ' in str
int findcharcount (CString str)
{
	int num=0;
	char* Strchar = (char *) malloc (sizeof (char));//Initialize Strchar strchar=
	(LPSTR) (LPCTSTR) str;//converts str to char* for
	( int I=0;I<STR. GetLength (); ++i)
	{
		if (strchar[i]== ', ')
		{
			++num;//if comma, num plus 1
		}
	}
	strchar=null;
	return num;
}

2. Get the values separated by commas (in string form), and the code is as follows:

int Count=findcharcount (CHILDMV);//count is the number of commas in CHILDMV CString
child_id;//Save the output string for
(int j=0;j< COUNT;++J)
{The
	//afxextractsubstring function is used to split the string, the first parameter holds the output string,
	//The second argument is the string to be separated, and the third parameter is the ordinal of the extracted string, starting from 0
	//The fourth parameter is for the split character, the default is ' \ n '
	Afxextractsubstring (Child_id,childmv,j, ', ');//child_id holds each comma-delimited number (string form)}
Afxextractsubstring (Child_id,childmv,j, ', '), and then add the code to process each of the obtained values
Related Article

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.