How do I get the last four characters from a string in C#?

前端 未结 20 1078
陌清茗
陌清茗 2020-11-30 18:13

Suppose I have a string:

\"34234234d124\"

I want to get the last four characters of this string which is \"d124\". I can use <

20条回答
  •  隐瞒了意图╮
    2020-11-30 18:51

    assuming you wanted the strings in between a string which is located 10 characters from the last character and you need only 3 characters.

    Let's say StreamSelected = "rtsp://72.142.0.230:80/SMIL-CHAN-273/4CIF-273.stream"

    In the above, I need to extract the "273" that I will use in database query

            //find the length of the string            
            int streamLen=StreamSelected.Length;
    
            //now remove all characters except the last 10 characters
            string streamLessTen = StreamSelected.Remove(0,(streamLen - 10));   
    
            //extract the 3 characters using substring starting from index 0
            //show Result is a TextBox (txtStreamSubs) with 
            txtStreamSubs.Text = streamLessTen.Substring(0, 3);
    

提交回复
热议问题