i know java API function charAt for example
String s="dato"
char r=s.charAt(0); r is equal d
but my question is how realy it works or what function can i use instead of charAt function to get same result? thanks
i know java API function charAt for example
String s="dato"
char r=s.charAt(0); r is equal d
but my question is how realy it works or what function can i use instead of charAt function to get same result? thanks
To what purpose? I mean, if charAt(0)
does what you want, why look for something else?
But, to answer your question: no, there is no (reasonably straight forward) way to get the nth character from a String in Java. You could use String
's substring(...)
method, but that returns a String
, not a char
.
Or first convert your String
into an array of char
's with String
'stoCharArray()
and get a character from the array, but that's just silly when you have charAt(...)
at your disposal.
There is no "function", there are only methods on objects, one object class is String
, and this class offers charAt()
for this purpose. Note that a String
is similar to an array of char
s, or a sequence of chars, and retrieving a character is just accessing the correct index in that sequence.
Note that it would be possible to get the first char in other ways (like converting the string to an array or whatever), but that wouldn't really make sense.