substr string_JavaScript String substr()方法与示例

substr string

字符串substr()方法 (String substr() Method)

substr() method is a string method in JavaScript, it is used to extract a part of the string (substring) from the given string.

substr()方法是JavaScript中的字符串方法,用于从给定的字符串中提取一部分字符串(子字符串)。

Syntax:

句法:

    String.substr(start, [length]);

Here,

这里,

  • String is the main string.

    字符串是主要字符串。

  • start is the initial index from where we have to extract the part of the string.

    start是我们必须从中提取字符串部分的初始索引。

  • length is an optional parameter, it defines the total number of characters to be extracted from the given index.

    length是一个可选参数,它定义了要从给定index中提取的字符总数。

Examples:

例子:

    str = "IncludeHelp is made for students.";
    start = 10
    length = None 
    Output:
    "p is made for students."

    Input:
    str = "IncludeHelp is made for students.";
    start = 0
    length = 11
    Output:
    "IncludeHelp"

Code:

码:

<html>
<head>
<title>JavaScipt Example</title>
</head>

<body>
<script>
	var str = "IncludeHelp is made for students.";
	var substring ="";
	
	substring = str.substr(10);
	document.write(substring + "<br>");

	substring = str.substr(0,11);
	document.write(substring + "<br>");	

	substring = str.substr(12,7);
	document.write(substring + "<br>");		
</script>
</body>
</html>

Output

输出量

p is made for students.
IncludeHelp
is made


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

substr string

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值