使用js子字符串选择第二个字符串点

selecting second string point using js substring

本文关键字:字符串 第二个 选择 使用 js      更新时间:2023-09-26

我想从长段落中选择一个字符串。它有若干个点('.')。我想把第二个单词剪掉,有办法吗?

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

从上面修剪的情况下,如果我使用indexOf它给出了第一个单词(一个),如果我使用lastIndex它给出了单词(三个),但我需要选择第二个,获得值为'second'

我如何使用indexOf方法修剪这个?或者选择像1这样的多组合字符串。三个或一个。二,还是二,三?

提前感谢!

use string.split.

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

的例子:

"".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"

这对你有用吗?

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]);

例子