locked
how to remove part of string after "/" RRS feed

  • Question

  • i have string like this "111/222/xyz" how can i remove the last part "/xyz" and keep only first 2 parts. the last part maybe 3 charactrs or more.

    thanks for any help.


    Thanks for any help :) my blog is: http://www.waelk.com

    Sunday, May 27, 2012 11:42 AM

Answers

  • Use LastIndexOf(), e.g.

    namespace ConsoleApplication6
    {
        using System;
        
        class Program
        {
            static void Main(string[] args)
            {
                string test = "111/222/xyz";
                test = test.Substring(0, test.LastIndexOf('/'));
                Console.WriteLine(test);
                Console.ReadLine();
            }
        }
    }
    

    • Proposed as answer by Kris444 Sunday, May 27, 2012 12:38 PM
    • Marked as answer by Alexander Sun Tuesday, May 29, 2012 9:34 AM
    Sunday, May 27, 2012 11:49 AM
  • An alternate way:

    var xParts = mystring.Split("/");
    if (xParts.Length > 1) {
      result = xParts[0] + "/" + xParts[1];
    }


    "Programming is an art form that fights back"

    • Marked as answer by Alexander Sun Tuesday, May 29, 2012 9:34 AM
    Sunday, May 27, 2012 1:52 PM
  •  Try This
     string value = "111/222/xyz";
     int index1 = value.LastIndexOf(@"/");
     string index3 = value.Substring(index1, value.Length - index1);
     string value1 = value.Replace(index3, "");
     Console.WriteLine(index1);
     Console.WriteLine(index3);
     Console.WriteLine("value1 "+ value1);
     Console.ReadLine();


    With Thanks and Regards Sambath Raj.C

    • Proposed as answer by Sambath Raj.C Monday, May 28, 2012 6:57 AM
    • Marked as answer by Alexander Sun Tuesday, May 29, 2012 9:34 AM
    Monday, May 28, 2012 6:49 AM

All replies

  • Typed freehand, not checked...
     
    string oldstring = "111/222/xyz";
    string newstring = oldstring.Substring(oldstring.LastIndexOf("/"));

    --
    Mike
    Sunday, May 27, 2012 11:44 AM
  • Yep, should have checked it...
     
    It should be:
     
        string newstring = oldstring.Substring(0, oldstring.LastIndexOf("/"));

    --
    Mike
    • Proposed as answer by Kris444 Sunday, May 27, 2012 12:38 PM
    Sunday, May 27, 2012 11:45 AM
  • thanks for your help but this code is keep the last part when i want to remove the last part and keep only first and second part

    Thanks for any help :) my blog is: http://www.waelk.com

    Sunday, May 27, 2012 11:47 AM
  • Use LastIndexOf(), e.g.

    namespace ConsoleApplication6
    {
        using System;
        
        class Program
        {
            static void Main(string[] args)
            {
                string test = "111/222/xyz";
                test = test.Substring(0, test.LastIndexOf('/'));
                Console.WriteLine(test);
                Console.ReadLine();
            }
        }
    }
    

    • Proposed as answer by Kris444 Sunday, May 27, 2012 12:38 PM
    • Marked as answer by Alexander Sun Tuesday, May 29, 2012 9:34 AM
    Sunday, May 27, 2012 11:49 AM
  • Nope, it removes the last part..
    Sunday, May 27, 2012 11:51 AM
  • An alternate way:

    var xParts = mystring.Split("/");
    if (xParts.Length > 1) {
      result = xParts[0] + "/" + xParts[1];
    }


    "Programming is an art form that fights back"

    • Marked as answer by Alexander Sun Tuesday, May 29, 2012 9:34 AM
    Sunday, May 27, 2012 1:52 PM
  • It was not clear to me if the original string was always going to be three "parts".  If so, this method is appropriate.

    --
    Mike
    Sunday, May 27, 2012 3:35 PM
  •  Try This
     string value = "111/222/xyz";
     int index1 = value.LastIndexOf(@"/");
     string index3 = value.Substring(index1, value.Length - index1);
     string value1 = value.Replace(index3, "");
     Console.WriteLine(index1);
     Console.WriteLine(index3);
     Console.WriteLine("value1 "+ value1);
     Console.ReadLine();


    With Thanks and Regards Sambath Raj.C

    • Proposed as answer by Sambath Raj.C Monday, May 28, 2012 6:57 AM
    • Marked as answer by Alexander Sun Tuesday, May 29, 2012 9:34 AM
    Monday, May 28, 2012 6:49 AM
  • I liked how you did it. Guess this will work too though it does not answer the question.

    var xParts = mystring.Split("/")[0] + "/" + mystring.Split("/")[1];



    developer

    Wednesday, May 30, 2012 2:54 PM
  • var xParts = mystring.Split("/")[0] + "/" + mystring.Split("/")[1];

    This is not a good way to do it. It calls the split function twice. Its better to store the result of split and use the result as I posted earlier.

    "Programming is an art form that fights back"

    Wednesday, May 30, 2012 9:41 PM