In this post I will be covering ways to go about getting a single char from a string, but also hopefully break ground into other ways of going about getting at what one might be after when it comes to getting something in a string, number, or any object.
I generally seem to prefer to just use the bracket syntax when getting at just a single char in a given string. The issue of it returning undefined may not be much of a draw back if I am doing something to prevent that from happening in the first place, which is what I should be doing anyway.
In addition the bracket syntax will not just work with strings, but it also works when getting a single element of an array, or the value of a named key when giving that named key as the value when using the bracket syntax. So just using the bracket syntax is a more robust option that the charAt method, but there are other options to that can be used to get a single char in a string.
So the charAt method alone with the bracket syntax are ways of getting a single char if you know the index value. However what if you do not know the index value to begin with? Then you would need some way to get one or more index values in a string first in order to know the index value to give. On top of that while you are at it should there be a way to just get the values to while we are at it?
So one way of getting an index value to begin with would be using the string index of method. If you know the single char, or the value of the string that you want to find inside of the string this method will work fine for getting the first index value from left to right.
This method will only get the first index value though, and it will not support the use of regular expressions rather than just a string value. SO with that said lets look at some additional examples of getting the index value before hand.
So one way to get an index value of the first instance of a pattern including a one char pattern would be to use the string match method. If the string match method is given a non global pattern then it will return an object that will contain an index property that is the index of the first instance of the given patter from left to right. However it will give an array of primitive values rather than objects with index values in the event it is given a pattern with the global flag set to true.
So the string match method works okay at getting a single index value, but not more than one. It will work okay if you just care about getting and array of values where each value in the array is the char that you are looking for though.
So then there is the exec method of the regular expression prototype object. This might be the best option for getting an array of index values the match with a given regular expression that can just be a single char, or any given pattern actually.
The string substr method is also there in the string prototype object for getting not just one char, but a whole range of chars. That is if you know both the starting index, and number of chars that you want to get from the string.
There is using the string methods to get a range of chars from a string rather than just a single char with charAT, but then there is using the array method splice also. One way that the array method splice can be used is by first converting the string into an array with the split string prototype method. Once I have an array of chars rather than a string I can then use the array splice method to return a new array from that array that is just a given set of chars from a given starting index value and a number of chars. I can then use the array join method to join the new array back into a string.
There are other ways of using array prototype methods such as using the function call method. That way of doing it involves just directly using array methods on a string that often work because strings are array like objects.
In any case the point here is that you are not limited to what can be done with the string prototype methods. Strings can be easily converted to arrays with split, and convert back with join. In some cases yu can just directly use the method of another prototype object using the function call method.