Solved

How to split this in C++

Posted on 2016-09-08
4
139 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:Mlungisi Ndlela
[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
  • 2
4 Comments
 
LVL 25

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 57

Expert Comment

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

Author Comment

by:Mlungisi Ndlela
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 57

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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
Simple Linear Regression
Introduction to Processes

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