How to get the first five character of a String

后端 未结 20 2362
醉酒成梦
醉酒成梦 2020-12-01 05:01

I have read this question to get first char of the string. Is there a way to get the first n number of characters from a string in C#?

20条回答
  •  盖世英雄少女心
    2020-12-01 05:23

    Or you could use String.ToCharArray().
    It takes int startindex and and int length as parameters and returns a char[]

    new string(stringValue.ToCharArray(0,5))
    

    You would still need to make sure the string has the proper length, otherwise it will throw a ArgumentOutOfRangeException

提交回复
热议问题