C# Substring Alternative - Return rest of line within string after character

前端 未结 5 560
离开以前
离开以前 2021-01-17 03:25

I have some code that reads a text file and sets it to a string. Each line of the text file contains separate data parameters that I a trying to extract. Here is an small ex

5条回答
  •  执笔经年
    2021-01-17 04:17

    Yes, you can do it much more concisely.

    string partNum = partData.Substring(partData.IndexOf("=") + 1);
    

    This uses the overloaded version of String.Substring that only accepts one parameter, the starting position in the string. It continues from that point to the end of the string.

    Clearly this only works if you're certain that there is equals sign in your partData, but so would the original code you posted.

提交回复
热议问题