+ 10

Index of a character in a string in an array

How can we get a character from a string kept in an array? arr = ["Bob", "Hello", "Sup"] For example, I want to get letter 'e' from arr[1] ("Hello") and I want the output to be '1' because the index of 'e' in 'Hello' is '1', Idk if that makes any sense but does anyone know?

31st Mar 2017, 1:05 PM
MrCoder
MrCoder - avatar
33 Respostas
+ 10
Refer to this code: https://code.sololearn.com/cDk92HnAZaOy/?ref=app Basically, arr[1] is equal to "Hello" That means arr[1].index('e') is equal to "Hello".index('e') hope you understood
31st Mar 2017, 2:26 PM
Supersebi3
Supersebi3 - avatar
+ 18
If it's in C++, where strings are character arrays themselves, I'd be doing: cout << (arr[1])[1]; I haven't tested yet. Will update shortly. EDIT: Yep, it works this way in C++. EDIT2: Yep, it is also working in Ruby. puts((arr[1])[1]) Looks like I should find some time to master Ruby. I've always had a liking to the language and looks like it likes me too. :> EDIT3: Thanks to @Supersebi for pointing out my flaws. The correct code should be: puts(arr1.index('e'))
31st Mar 2017, 1:12 PM
Hatsy Rei
Hatsy Rei - avatar
+ 17
puts(arr[1].index('e')) Just off the top of my head. Maybe put it in a for loop.
31st Mar 2017, 2:47 PM
Jafca
Jafca - avatar
+ 16
@SuperSebi3 It's alright. @Hatsy Rei won't get it wrong any more
31st Mar 2017, 5:36 PM
Jafca
Jafca - avatar
+ 16
@supersebi3 Oops, my bad
31st Mar 2017, 5:38 PM
Jafca
Jafca - avatar
+ 15
@Supersebi3 Sorry for getting your name wrong. I'm personally a victim too (Hasty Rei, Hatsey Rei) so I understand.
1st Apr 2017, 8:58 AM
Hatsy Rei
Hatsy Rei - avatar
+ 15
@MrCoder looks like someone hasn't updated their SoloLearn app :P
1st Apr 2017, 9:01 AM
Hatsy Rei
Hatsy Rei - avatar
+ 11
I have to choose one of you to be the best answer! :O
1st Apr 2017, 9:05 AM
MrCoder
MrCoder - avatar
+ 10
Oh alright @Jafca @Hatsy Rei @Luka @Supersebi3 Thank you very much :D PS: Dont fight here lol
1st Apr 2017, 12:37 AM
MrCoder
MrCoder - avatar
+ 10
@Hatsy Rei Ohh... Woops didn't get a notification about itšŸ˜‚ Hold on buddies
1st Apr 2017, 9:02 AM
MrCoder
MrCoder - avatar
+ 9
@Hatsy Rei why do I have to tell this every community I enter... IM Supersebi3 NOT SuperSebi3!!!!!! THE SECOND s IS SMALL
31st Mar 2017, 5:34 PM
Supersebi3
Supersebi3 - avatar
+ 9
@MrCoder mark best answer pls
1st Apr 2017, 8:56 AM
Supersebi3
Supersebi3 - avatar
+ 9
@Hatsy Rei :)
1st Apr 2017, 8:59 AM
Supersebi3
Supersebi3 - avatar
+ 9
Anyone thinking of a random generator(āŒ Ķ”ā–  ĶœŹ– Ķ”ā– )
1st Apr 2017, 9:06 AM
MrCoder
MrCoder - avatar
+ 9
@SATYAJEET JHA thats how it works in ruby too, but he wanted to get the index number of 'e' in "Hello", not the character and this is solved anyways
20th Apr 2017, 8:29 PM
Supersebi3
Supersebi3 - avatar
+ 8
@Jafca but you did šŸ˜”šŸ˜”šŸ˜”šŸ˜”
31st Mar 2017, 5:37 PM
Supersebi3
Supersebi3 - avatar
+ 8
@jaFca small first s is okay
31st Mar 2017, 5:40 PM
Supersebi3
Supersebi3 - avatar
+ 8
@Supersebi3 I dont even know how but I wanted to
1st Apr 2017, 9:00 AM
MrCoder
MrCoder - avatar
+ 8
@MrCode press the checkmark below the like/dislike buttons
1st Apr 2017, 9:01 AM
Supersebi3
Supersebi3 - avatar
+ 8
also I updated my code to have a fourth way without any variable
1st Apr 2017, 9:06 AM
Supersebi3
Supersebi3 - avatar