JavaScript中的String substring()方法和示例

JavaScript | 字符串substring()方法 (JavaScript | String substring() Method)

The String.substring() method in JavaScript is used to return a part of the string. The substring() extracted is from the given start and end index of the string.

JavaScript中的String.substring()方法用于返回字符串的一部分。 提取的substring()来自给定的字符串开始结束索引。

Syntax:

句法:

    string.substring(startIndex, endIndex);

Parameter(s):

参数:

The method accepts two parameters, they are the starting and ending index of the substring string which is to be extracted.

该方法接受两个参数,它们是要提取的子字符串字符串的开始和结束索引。

Return value:

返回值:

The return type of this method is string which is the substring output of the given string.

此方法的返回类型是string ,它是给定字符串的子字符串输出。

Browser support: Chrome, Internet explorer, Mozilla, Safari, Opera mini.

浏览器支持: Chrome,Internet Explorer,Mozilla,Safari,Opera mini。

Example 1:

范例1:

<script>
	document.write("Hello".substring(1,3) + "<br/>");
	document.write("Hello".substring(0,2) + "<br/>");
	document.write("Hello".substring(0,5) + "<br/>");
	document.write("Hello".substring(0,8) + "<br/>"); //no error
	document.write("Hello World!".substring(6,12) + "<br/>");
</script>

Output

输出量

el
He
Hello
Hello
World!

Example 2:

范例2:

<script>
	str = "www.includehelp.com";

	document.write(str.substring(0,3) + "<br/>");
	document.write(str.substring(4,15) + "<br/>");
	document.write(str.substring(16,19) + "<br/>");
	document.write(str.substring(0) + "<br/>");
	document.write(str.substring(4) + "<br/>");
	document.write(str.substring(16) + "<br/>");
</script>

Output

输出量

www
includehelp
com
www.includehelp.com
includehelp.com
com


翻译自: https://www.includehelp.com/code-snippets/string-substring-method-with-example-in-javascript.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值