C# - Substring: index and length must refer to a location within the string

后端 未结 8 1363
有刺的猬
有刺的猬 2020-12-05 23:31

I have a string that looks like

string url = \"www.example.com/aaa/bbb.jpg\";

\"www.example.com/\" is 18 fixed in length. I want to get th

8条回答
  •  情歌与酒
    2020-12-05 23:42

    Your mistake is the parameters to Substring. The first parameter should be the start index and the second should be the length or offset from the startindex.

    string newString = url.Substring(18, 7);
    

    If the length of the substring can vary you need to calculate the length.

    Something in the direction of (url.Length - 18) - 4 (or url.Length - 22)

    In the end it will look something like this

    string newString = url.Substring(18, url.Length - 22);
    

提交回复
热议问题