?
Solved

How to split this in C++

Posted on 2016-09-08
4
Medium Priority
?
158 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 1000 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 59

Expert Comment

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

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 59

Assisted Solution

by:Julian Hansen
Julian Hansen earned 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

649 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