Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I have a input string in C# like
string Str="L24789A-12345Ax.DAT";


now I have to separate the Str like
string Str1,Str2
str1="L24789A";
Str2="12345Ax";


There should not come .DAT

Can any one help me please
Thanks to All.
Posted
Updated 3-Jul-11 20:20pm
v2
Comments
PSK_ 4-Jul-11 2:17am    
"L24789A-12345Ax.DAT" is this a file name?
IndrajitDasgupat 4-Jul-11 2:24am    
its like a string not file name because my client will get it as string only

I would use regular expressions, as in this case you want to split on one thing, and also remove another. It IS true that Path.GetFilenameWithoutExtension will remove the .DAT for you and you could just split on the - after that.

OK, I would use ([^-]*)-([^.]*).DAT as the regex, and then when you run it, the two captured groups will be the two strings you want. You can replace DAT with something more general to match any filename.
 
Share this answer
 
v3
Comments
yesotaso 4-Jul-11 2:36am    
Indeed regex has all the tools in capable hands to chop the strings. +5
StM0n 4-Jul-11 2:41am    
I rather recommend an independent solution... so I would also go with the regex.
Try like following code snippet.

C#
string Str = "L24789A-12345Ax.DAT";
string strWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(Str);
string[] splitArray = strWithoutExtension.Split('-');
string firstPart = splitArray[0];
string secondPart = splitArray[1];
 
Share this answer
 
Comments
Christian Graus 4-Jul-11 2:34am    
Doesn't solve his issue.
Tarun.K.S 4-Jul-11 3:18am    
Nice one. 5+
IndrajitDasgupat 4-Jul-11 5:30am    
it is working fine.
Thanks
It may help you because this solution will be the best by considering performance as well:

static void Main(string[] args)
        {
            string a = "L24789A-12345Ax.DAT";
            string[] b = (a.Replace(".DAT", "")).Split('-');
            Console.WriteLine("first = " + b[0]);
            Console.WriteLine("second = " + b[1]);
            Console.ReadLine();
        }
 
Share this answer
 
v2
Comments
Tarun.K.S 4-Jul-11 3:18am    
Right. 5+
Use:

C#
string str="L24789A-12345Ax.DAT"; //for example
//...

string parts[] = str.Split(new char[] {'-', '.'});
//it will give you (with data in your example)
//array with 3 elements: use <code>parts[0]</code> and <code>part[1]</code>;
//they will contain strings you need;
//ignore part[2] where "DAT" is.


—SA
 
Share this answer
 
v3
Comments
IndrajitDasgupat 4-Jul-11 2:11am    
What about .DAT because I don't need .DAT so how to remove that from the string
Sergey Alexandrovich Kryukov 5-Jul-11 0:30am    
I did not pay attention, sorry. OK, now it is taken into account, please see the updated solution.
--SA
You can use substring to remove the dat file and then split it.
 
Share this answer
 
Just insert following few lines of code to code provided by SA:
string str="L24789A-12345Ax.DAT"; //for example
int len = str.Length;
string str1 = str.Substring(1, len - 5);
string[] parts = str1.Split(new char[] {'-'});
label1.Text = parts[0];
label2.Text = parts[1];
 
Share this answer
 
Comments
Christian Graus 4-Jul-11 2:34am    
SO ugly. Why not write code that will return the right value, no matter what ?

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