Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi ,

I have a string which i want to split.I am retrieving this string from database.I have around 100 strings which i am capturing
for example:
\\C-IMS\IMSData\Images\F1231231\3eb8b6e4-9792-4328-a246-0766e205d0a5.JPG

\\C-IMS\IMSData\Images\F5477748\97cf42d9-3ed1-4d0c-a26d-098e063871e3.JPG

Now I want to split each string upto \\C-IMS\IMSData\Images\F1231231 and \C-IMS\IMSData\Images\F5477748.

Thanks in advance

Regards,
richa
Posted

You could solve that using LINQ. Assuming your strings are stored in an IEnumerable or similar you could do the following:
C#
// string[] stringCollection = { .. }
var query = from element in stringCollection
            select element.Substring(0, element.LastIndexOf('\\'));

foreach (var item in query)
{
     // Further processing could happen here
}
 
Share this answer
 
v2
here is non linq way

C#
string str=@"\\C-IMS\IMSData\Images\F1231231\3eb8b6e4-9792-4328-a246-0766e205d0a5.JPG";

      string str1 = @"\\C-IMS\IMSData\Images\F5477748\97cf42d9-3ed1-4d0c-a26d-098e063871e3.JPG";

        string[] strarr=new string[2];
        strarr[0] = str;
        strarr[1] = str1;
        for (int i = 0; i < strarr.Length; i++)
        {
            string newstr = strarr[i].Substring(0, strarr[i].LastIndexOf('\\'));
            MessageBox.Show(newstr);
        }
 
Share this answer
 

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