Solved

How to split this in C++

Posted on 2016-09-08
4
76 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 51

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 51

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
A short article about problems I had with the new location API and permissions in Marshmallow
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

707 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

15 Experts available now in Live!

Get 1:1 Help Now