0
How to compare a string's 1st character with a integer in java
Example String s="1234" int i =1 I want to compare s[0] with i
5 Antworten
+ 5
boolean result = ( Integer.valueOf( s.charAt( 0 ) ) - '0' == i );
This assumes string <s> contains only digits.
+ 6
Rishab Solanki
As you know that String is a character of sequence and also String is a class which has method charAt(index) so you can get 1st character using charAt(0)
Since it will return character not integer so you need to get is as a integer using Integer.valueOf() method.
If you want to get 1st character using s[0] then you need to convert String in character array
+ 1
Can you describe the goal of the code you are attempting to write?
+ 1
Thank you😊
0
I have explained in example