Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 491 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C#: how to get first char of a string?

#1
Can the first `char` of a string be retrieved by doing the following?

MyString.ToCharArray[0]
Reply

#2
Just `MyString[0]`. This uses the [String.Chars][1] indexer.


[1]:

[To see links please register here]

Reply

#3
`Mystring[0]` should be enough
Reply

#4
I think you are looking for this `MyString.ToCharArray()[0]`

:)

But you can use `MyString[0]` too.
Reply

#5
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.
Reply

#6
Or you can do this:

MyString[0];

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through