Click here to Skip to main content
15,924,452 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<pre>Hello I am new in c# and trying to split string. here is my string=
"[)>_d03006_d029VTCUP_d029P2865478547_d029T190917U111@9E005_d029_d030_d004"

GS is _d029

RS is _d030

EOT is _d004

and output should be 

[)>RS06

TCUP

2865478547

190917

U111

@

9E005

RSEOT


please help...thanks in advanced...!!


What I have tried:

C#
<pre>  var input = "[)>_d03006_d029VTCUP_d029P2865478547_d029T190917U111@9E005_d029_d030_d004";
            var split = input.Split(new string[] { "_d029V","_d029P","_d029T" }, StringSplitOptions.RemoveEmptyEntries);

            label1.Text = split[0];
            label2.Text = split[1];
            label3.Text = split[2];
            label4.Text = split[3];
Posted
Updated 18-Oct-19 5:21am
Comments
RickZeeland 18-Oct-19 9:51am    
Are you sure your input string is not missing some separator strings ?
F-ES Sitecore 18-Oct-19 9:51am    
Without knowing the rules for how the string is made it is literally impossible to answer this. For example if the string length remains the same then use Substring to read the portions you need. If it is more complex than that then you'll need to provide more details.

I have a feeling that this is part of a bigger solution; and there may be a better solution to this whole thing.... If we only knew what the big picture was.

I do recognize the basics of your separators, as they are ASCII Control Characters.
1. RS is Record Separator. This is similar to a Row
2. GS is Group Separator, this is analogous to a Column
3. EOT is End of Transmission. This and everything after is to be ignored

If we put this into XML you would end up with this structure
XML
<RS>
	06
	<GS>VTCUP</GS>
	<GS>P2865478547</GS>
	<GS>T190917U111@9E005</GS>
</RS>
Based on this, I would recommend splitting multiple times:
The first pair of splits should be at the Control Chars (RS, GS)

Now it gets tricky:
1. The first characters after the RS are not Grouped; could this be a record ID?

2. The first character after the GS you are pulling out as well in your code
... Is this an attribute or similar (column name?). Maybe take SubString(0,1)?

3. You also have an undefined split (@) in your output, but not in code
 
Share this answer
 
var input = "[)>_d03006_d029VTCUP_d029P2865478547_d029T190917U111@9E005_d029_d030_d004";
input = input.Replace("_d029_", "#_").Replace("_d030", "RS").Replace("U111@", "U111#@#").Replace("_d004", "EOT");
var split = input.Split(new string[] { "_d029V", "_d029P", "_d029T", "#" }, StringSplitOptions.RemoveEmptyEntries);

foreach (var item in split)
{
    Debug.Print(item);
}
 
Share this answer
 
v2
Comments
Sam_gaik 18-Oct-19 10:24am    
Thanks it works....now how to show each output to lable?
RickZeeland 18-Oct-19 12:28pm    
You already have the code for that, use label1.Text = split[0]; etc.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900