Solved

How to split this in C++

Posted on 2016-09-08
4
93 Views
Last Modified: 2016-10-06
Hi Experts. I'm new to C++. I would like to know how can I split this and assign it to variables.
X56345521^Surname/Initials^542153120000000000000000?

Open in new window


I want to extract it like this:

Variable1 = X56345521
Variable2 = Surname
Variable3 = Initials
Variable4 = 542153
Variable5 = 2000000000

I think the best way for Surname and Initials is to get them both as an array then split it up which I think will be a bit easy to get as its has special characters, but already have a code to get it as an array,  now need to assign each data to a correct variable.

The code I have is:
array<String^>^ ParseMyData(String^ data)
 {
     String^ tempData = data->Split(gcnew String('^',1)->ToCharArray())[1];
     return tempData->Split(gcnew String('/',1)->ToCharArray());
 }

Open in new window


Thank you.
0
Comment
Question by:MCSI Developers
  • 2
4 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 250 total points
ID: 41790699
FYI: you can pass multiple delimiters to the Split function:
return data Split(gcnew String("^/")->ToCharArray());

Open in new window

0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 41790753
What is the rule for splitting on the 2000000000 ?
0
 
LVL 1

Author Comment

by:MCSI Developers
ID: 41792239
Hi, I think it will be better if we ignore the 2000000000 because some devices doesn't support IDC writing so, I've thought of another way around so, extracting 2000000000 is no longer impotent as I will get this with another method of using a database.

The reason is that this data will not always the same in length so that might produce other internal errors. But still need to know on how I can get these:
Variable1 = X56345521
Variable2 = Surname
Variable3 = Initials
Variable4 = 542153

* The rule for getting the first data is that you get all the data on the left hand side before the first ^ sign.
* The rule for getting Surname is that you get the data after the first ^ sign and before the / sign.
* The rule for getting the Initials is that you get the data after the / sign and before the ^ sign.
* The rule for getting the last variable data is that you get the first 6 digits after the last ^ sign or after the second ^ sign

Thank you.
0
 
LVL 52

Assisted Solution

by:Julian Hansen
Julian Hansen earned 250 total points
ID: 41792275
Then chaau seems to have provided the answer. You can call split with multiple separators. This returns an array. You using the indices of the elements you want to access their values.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

930 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now