简体   繁体   中英

How to cut third symbol from string and alert it without third symbol

How can i correctly cut out letter "v" and alert str without "v"

var str = "Javascript";
var cut = str.substring(2,3);

You're using the right tool ( String#substring ). You need two substrings that you put back together, the "Ja" and the "ascript" . So:

 var str = "Javascript"; var cut = str.substring(0, 2) + str.substring(3); alert(cut); 

Another option would be String#replace , which will replace the first occurrence of what you give it unless you tell it to do it globally with a regex and the g flag (which we won't, because we just want to remove that one v ):

 var str = "Javascript"; var cut = str.replace("v", ""); alert(cut); 


Just for fun, there is another way, but it's a bit silly: You can split the string into an array of single-character strings, remove the third entry from the array, and then join it back together:

 var str = "Javascript"; var cut = str.split("").filter(function(_, index) { return index != 2; }).join(""); alert(cut); 

or

 var str = "Javascript"; var cut = str.split(""); cut.splice(2, 1); // Delete 1 entry at index 2 cut = cut.join(""); alert(cut); 

...but again, that's a bit silly. :-)

var str = "Javascript";
var cut = str.substring(0,2) + str.substring(3);
alert(cut);

use replace method

 var str = "Javascript"; str = str.replace("v", ""); alert(str); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM