.NET equivalent of the old vb left(string, length) function

后端 未结 11 1837
误落风尘
误落风尘 2020-12-02 17:50

As a non-.NET programmer I\'m looking for the .NET equivalent of the old Visual Basic function left(string, length). It was lazy in that it worked for any lengt

11条回答
  •  北荒
    北荒 (楼主)
    2020-12-02 18:28

    If you want to avoid using an extension method and prevent an under-length error, try this

    string partial_string = text.Substring(0, Math.Min(15, text.Length)) 
    // example of 15 character max
    

提交回复
热议问题