How to use Javascript slice to extract first and last letter of a string?

get last character of string javascript
charat javascript
javascript slice string
js substring
javascript split
javascript string replace
string slice python
javascript substring vs substr

How to use JavaScript slice to extract the first and last letter of a string?

Eg: "Hello World"

I need the result as "dH".

Following is my jsfiddle :

http://jsfiddle.net/vSAs8/

Here's the cleanest solution :

var output = input.slice(-1)+input[0];

If you want more slice, there's also

var output = input.slice(-1)+input.slice(0,1);

And here are alternate fun (and less efficient) solutions :

var output = input.replace(/^(.).*(.)$/,'$2$1');

or

var output = input.match(/^.|.$/g).reverse().join('');

JavaScript String slice() Method, The slice() method extracts parts of a string and returns the extracted parts in a new string. Use the start and The first character has the position 0, the second has position 1, and so on. Tip: Use a Example. Extract only the last character:. The slice () method extracts parts of a string and returns the extracted parts in a new string. Use the start and end parameters to specify the part of the string you want to extract. The first character has the position 0, the second has position 1, and so on. Tip: Use a negative number to select from the end of the string.

Substr works as well:

alert(test.substr(-1,1) + test.substr(0,1));

JavaScript String charAt() Method, The index of the first character is 0, the second character is 1, and so on. Tip: The index of the last character in a string is string.length-1, the second last  To summarize, charAt() and slice() will help return string values based on index numbers, and indexOf() and lastIndexOf() will do the opposite, returning index numbers based on the provided string characters. Finding the Length of a String. Using the length property, we can return the number of characters in a string. "How are you?".length;

a.charAt(a.length-1) + a.charAt(0) 

String.prototype.slice(), As an example, str .slice(2, -1) extracts the third character through the second to last character in the string. Examples. Using slice()  The first position in string is 0. If a negative value is provided for this parameter, the slice () method will measure the position from the end of the string. For example, a value of -1 is the last character in the string, a value of -2 is the second last character in the string and so on.

var str = " Virat Kohali " 
var get_string_label = function(str){ 
  str = str.split(" "); 
  str = str.filter(res=>res.length>0); 
  str = str.map(function(res){ 
    return res[0].toUpperCase(); 
  }); 
  str = str.join(""); 
  return str; 
}; 
console.log(get_string_label(str));

str.split(" "); method splits a String object into an array of strings by separating the string into substrings, where it will find space in string.

then str.filter(res=>res.length>0) will filter out string having zero length (for "virat kohali" string you will get empty sub-string) after that using map function you can fetch your first letter

String.prototype.substring(), indexStart: The index of the first character to include in the returned substring. and length property to extract the last characters of a particular string. The substring() and slice() methods are almost identical, but there are a  JavaScript Array subString, Slice & More. JavaScript has native members of the String object to help extract sub-strings based on character position: substring, substr and slice. Each return a string, extracted from an initial string based on start position and a length. On the surface they look almost identical, but have some subtle differences.

How to get the last character of a string in JavaScript , First count number of characters in a given string by using str.length function. Example: This example uses slice() function to get the last character of string. To capitalize the first letter of a random string, you should follow these steps: 1. Get the first letter of the string; 2. Convert the first letter to uppercase; 3. Get the remainder of the string; 4. Concatenate the first letter capitalized with the remainder of the string and return the result; 1.

Get last n characters of a string in JavaScript, There are several methods to get last n characters from a string using JavaScript native methods substring() and slice() . 1. String.prototype.substring(). The  This method extracts the characters in a string between "start" and "end", not including "end" itself. If "start" is greater than "end", this method will swap the two arguments, meaning str.substring (1, 4) == str.substring (4, 1). If either "start" or "end" is less than 0, it is treated as if it were 0.

JavaScript: The Missing Manual: The Missing Manual, One way to do this is to extract every character in the string that follows the would be 0 (the first letter) and the last would be 0 + 10 or just 10:slice(0,10). The slice() Method. slice() extracts a part of a string and returns the extracted part in a new string. The method takes 2 parameters: the start position, and the end position (end not included). This example slices out a portion of a string from position 7 to position 12 (13-1):

Comments
  • Wouldn't something like str[str.length-1] + str[0] be enough here?
  • s.substring(s.length-1)+s.substring(0,1);
  • any solution in 1 step?
  • What does that mean "1 step" ? input.slice(-1)+input[0] is one expression, is very fast and one of the shortest possible.
  • One small caveat: accessing string characters via numeric properties is non-standard prior to ECMAScript 5. In practice, it's unsupported in IE <= 7. Therefore input.slice(-1)+input.charAt(0) would be safer.
  • @TimDown IE9 isn't supported anymore even by Google Mail. Supporting IE7 today would prevent so much of what we do that it's not worth trying.
  • It entirely depends on context. The reality for a significant number of web developers is that IE 7 remains in use (in some large corporations and schools, for example) and they have to deal with it. I agree it's becoming less of a concern, but it's not difficult in this case to head off a potential problem for the minority who do have to deal with IE 7 (which includes me, unfortunately).
  • use format options and add explanation to your answer. Don't just write codes
  • Thanks for advice @Harsh. Please find the edited code