Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: how to get first char of a string?

Tags:

string

c#

People also ask

Bahasa C digunakan untuk apa?

Meskipun C dibuat untuk memprogram sistem dan jaringan komputer namun bahasa ini juga sering digunakan dalam mengembangkan software aplikasi. C juga banyak dipakai oleh berbagai jenis platform sistem operasi dan arsitektur komputer, bahkan terdapat beberepa compiler yang sangat populer telah tersedia.

Apa yang dimaksud dengan huruf C?

C adalah huruf ketiga dalam alfabet Latin. Dalam bahasa Indonesia, huruf ini disebut ce (dibaca [tʃe]).


Just MyString[0]. This uses the String.Chars indexer.


Mystring[0] should be enough


Try this,

string s1="good"; string s=s1.Substring(0,1);


You can use LINQ

char c = mystring.FirstOrDefault()

It will be equal to '\0' if the string is empty.


The difference between MyString[0] and MyString.ToCharArray()[0] is that the former treats the string as a read-only array, while ToCharArray() creates a new array. The former will be quicker (along with easier) for almost anything where it will work, but ToCharArray can be necessary if you have a method that needs to accept an array, or if you want to change the array.

If the string isn't known to be non-null and non-empty you could do:

string.IsNullOrEmpty(MyString) ? (char?)null : MyString[0]

which returns a char? of either null or the first character in the string, as appropriate.