开发者

selecting second string point using js substring

开发者 https://www.devze.com 2023-03-29 12:20 出处:网络
i want to select a sting from the long para. it has number of dot(\'.\')s. i want to trim the word from the second one, is it any way to do this?

i want to select a sting from the long para. it has number of dot('.')s. i want to trim the word from the second one, is it any way to do this?

exam开发者_如何学JAVAple

var name = "one.two.three";
name.substring(0,name.indexOf('.'))
name.substring(0,name.lastIndexOf('.'))

from above trimming in case if i use indexOf it gives first word (one), if i use lastIndex of it gives the word (three), but i need to select the second one, to get value as 'second'

how can i trim this using indexOf method? or to select multicombination strings like one.three or one.two, or two.three?

thanks in advance!


use string.split.

e.g.

name.split(".")[1]


var name="one.two.three";
var result=name.split(".").slice(0,2).join(".");

Example:

"".split(".").slice(0,2).join(".") // return ""
"one".split(".").slice(0,2).join(".") // return "one"
"one.two".split(".").slice(0,2).join(".") // return "one.two"
"one.two.three".split(".").slice(0,2).join(".") // return "one.two"
"one.two.three.four.five".split(".").slice(0,2).join(".") // return "one.two"


Is that work for you ?

var name = "one.two.three";
var params = name.split('.');
console.log(params[1]);


use Split

var name = "one.two.three";

var output = name.split('.');

alert(output[1]);

example here

0

精彩评论

暂无评论...
验证码 换一张
取 消