I would like to know if it is possible to take a string and turn it an array, and also check its 5th letter. Like "I am walking on the street"
, check if its 5th letter is "z"
,for example.
Asked
Active
Viewed 392 times
-4

theMagicOne
- 1
- 1
-
2You can do this without converting to an array. There are multiple ways to do this without making an array. One of them is `.charAt()`. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt – gen_Eric Jan 14 '15 at 19:03
-
2`var mystring = "I am walking"; alert(mystring[5]);` – Jan 14 '15 at 19:04
-
2http://stackoverflow.com/help/how-to-ask – Ruan Mendes Jan 14 '15 at 19:05
-
possible duplicate of [How do I split a string, breaking at a particular character?](http://stackoverflow.com/questions/96428/how-do-i-split-a-string-breaking-at-a-particular-character) – Ryan Jan 14 '15 at 20:22
3 Answers
2
You can use String charAt. But it's zero based so, the first position is zero.
"I am walking on the street".charAt(4); // Get 5th letter
About the array transformation. You can use split function
"I am walking on the street".split(''); // will return ['I',' ','a','m', ...]

nanndoj
- 6,580
- 7
- 30
- 42
-
-
this yields a " " character...cause it takes into account spaces. OP wants letters only...the 5th letter – indubitablee Jan 14 '15 at 19:07
-
@indubitablee The question is not clear enough to indicate that spaces should be skipped, a space is a character too – Ruan Mendes Jan 14 '15 at 19:08
-
-
@JuanMendes actually the string I would use has no spaces. Does it changes the way .split() will be used? – theMagicOne Jan 14 '15 at 19:15
-
-
@indubitablee The OP's last comment proves that spaces are irrelevant :) – Ruan Mendes Jan 14 '15 at 19:17
-
@JuanMendes just because OP changed the scope of the requirements doesnt prove that spaces are irrelevant. The code written and suggested should be solid code and maintainable. With your high reputation, you would agree. This answer only works without spaces. It doesn't prove to be true with the example it uses "I am walking on the street" as .charAt(4) results in a space character and the 5th letter is "a" – indubitablee Jan 14 '15 at 19:26
-
@indubitablee My point is that the OP said letter, but really meant character. The question was kind of vague and you were making a requirement about something that the OP wasn't requiring. – Ruan Mendes Jan 14 '15 at 19:29
-
@nanndoj That's doubtful as the OP has no spaces in their string. – Spencer Wieczorek Jan 14 '15 at 19:35
1
You don't need to use an array, we can just check the index:
if(str.charAt(5) == 'z')
// It is
else
// It's not
Note that this includes a 0th
letter. If you start counting with 1
then use (4)
instead. This would also count spaces, if you want to only include counting letters where for example "H e l l o"
where o
is the 8th letter then you need to remove the spaces from the string first:
var stringWithoutSpace = str.replace(/ /g,'');
if(stringWithoutSpace.charAt(5) == 'z')
// It is
else
// It's not

Spencer Wieczorek
- 21,229
- 7
- 44
- 54
-1
First, its not necessary to create an array, but if you want to you can do
var string = "This is my string";
string.split("");
And then check the letter with
string[4];//5th letter
You can also use the charAt() statement, like
string.charAt(4);//5th letter

gabzerbinato
- 68
- 1
- 1
- 13
-
2Please, remove the space in string.split(" "); It will not split letters but words. And string[4] will return undefined! – nanndoj Jan 14 '15 at 19:28
-
-
You are spliting the string by spaces. string[0] = "This", string[1] = "is", string[2] = "my", string[3] = "string", string[4] = undefined. Look http://jsfiddle.net/e96tyezn/ – nanndoj Jan 14 '15 at 19:56
-
My intention was to write `.split("")`, but I wrote it wrong. I was considering that the output would be `["T","h","i","s"...]` – gabzerbinato Jan 14 '15 at 19:58
-
@gabzerbinato Yes I know, I was pointing out that your fixed the error. – Spencer Wieczorek Jan 14 '15 at 19:59