Home >Web Front-end >JS Tutorial >How to implement the js interception string function

How to implement the js interception string function

韦小宝
韦小宝Original
2018-01-17 10:36:132121browse

This article mainly introduces in detail the implementation method of js interception string function. It has certain reference and learning value of js. Friends who are interested in js can refer to this article.

There are 2 ways for js to intercept strings: substring(), slice(), for your reference, the specific content is as follows

The example given here is time.

css file:

body{ text-align:center} 
.con{ 
 margin:100px auto; 
 width:800px; 
 height:400px; 
 border:2px solid #336666;
 border-radius:5px;
 padding-top: 50px;
}


<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title>截取字符串</title>
  <script src="../js/jquery-1.10.2.js" type="text/javascript"></script>
  <link href="../css/commons.css" rel="external nofollow" rel="stylesheet" type="text/css" />
 </head>
 <body>
   
  <p class="con">
    时间:<input type=&#39;text&#39; id=&#39;time&#39; name=&#39;time&#39; value="2017-07-01 00:00:00" /><br><br>
    substring结果:<input type=&#39;text&#39; id=&#39;sub_result&#39; name=&#39;sub_result&#39; value="" /><br><br>
    slice结果:<input type=&#39;text&#39; id=&#39;sli_result&#39; name=&#39;sli_result&#39; value="" /><br><br>
    <button>提交</button>
  </p>
   
 <script>
  $("button").click(function(){
   var time_val = $("#time").val();
   
   var sub_res = time_val.substring(5, 10);
   $("#sub_result").val(sub_res);
   
   var sli_res = time_val.slice(5, 10);
   $("#sli_result").val(sli_res);
  });
  
  var stmp = "2017-07-01 00:00:00";
   //使用一个参数
//  alert(stmp.slice(5, 10));//从第3个字符开始,截取到最后个字符;返回"nn.cn"
//  alert(stmp.substring(5, 10));//从第4个字符开始,截取到最后个字符;返回"nn.cn"
//
//  //使用两个参数
//  alert(stmp.slice(1,5))//从第2个字符开始,到第5个字符;返回"cinn"
//  alert(stmp.substring(1,5));//从第2个字符开始,到第5个字符;返回"cinn"
//
//  //如果只用一个参数并且为0的话,那么返回整个参数
//  alert(stmp.slice(0));//返回整个字符串
//  alert(stmp.substring(0));//返回整个字符串
 </script>
 </body>
</html>

The above is the entire content of this article. I hope it will be helpful to everyone’s study. I also hope that everyone will support the PHP Chinese website. .

Related recommendations:

javascript Determine whether the user has operated the page

Detailed explanation of multiple inheritance examples in JavaScript

JavaScript to implement quick sort analysis

The above is the detailed content of How to implement the js interception string function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn