locked
Extract String RRS feed

  • Question

  • User-1499457942 posted

    Hi

      From below code i want to extract string from last / character. In below code i want Excel.txt

    http://localhost/Excel.txt

    Thanks

    Thursday, December 20, 2018 10:30 AM

Answers

  • User-1174608757 posted

    Hi JagjitSingh

    According to your description,I have made a sample here.I suggest you to use LastIndexOf("/") to get the index of the last occurrence within the entire,then  you can use .substring( ) to intercept the string from the index you get .Here is the code ,I hope it can help you.

    lastindex.aspx.cs:

    public partial class lastindex : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                string s = "http://localhost/Excel.txt";
                string s1 = s.Substring(s.LastIndexOf("/")+1);
                Response.Write(s1);
            }
        }

    You will see :

    Best Regards

    Wei Zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 21, 2018 3:17 AM

All replies

  • User-943250815 posted

    If string is always in URL/URI

    New Uri("http://localhost/excel.txt").AbsolutePath.Replace("/", "")



    Thursday, December 20, 2018 12:40 PM
  • User-1174608757 posted

    Hi JagjitSingh

    According to your description,I have made a sample here.I suggest you to use LastIndexOf("/") to get the index of the last occurrence within the entire,then  you can use .substring( ) to intercept the string from the index you get .Here is the code ,I hope it can help you.

    lastindex.aspx.cs:

    public partial class lastindex : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                string s = "http://localhost/Excel.txt";
                string s1 = s.Substring(s.LastIndexOf("/")+1);
                Response.Write(s1);
            }
        }

    You will see :

    Best Regards

    Wei Zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 21, 2018 3:17 AM