JS截取字元串的三種方法詳解
- 學習教育
- 關注:1.76W次
<link rel="stylesheet" href="https://js.how234.com/third-party/SyntaxHighlighter/shCoreDefault.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><script type="text/javascript"> SyntaxHighlighter.all(); </script>
JS提供三個截取字元串的方法,分別是:slice()
,substring()
和substr()
,它們都可以接受一個或兩個參數:
var stmp = "rcinn.cn";
使用一個參數
alert(stmp.slice(3));//從第4個字元開始,截取到最後個字元;返回"nn.cn"
alert(stmp.substring(3));//從第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));//返回整個字元串
返回第一個字元
alert(stmp.slice(0,1));//返回"r"
alert(stmp.substring(0,1));//返回"r"
//在上面的例子中我們可以看出slice()和substring()的用法是相同的,返回的值也是一樣的,但當參數爲負數時,他們的返回值卻不一樣
,看下面的例子
alert(stmp.slice(2,-5));//返回"i"
alert(stmp.substring(2,-5));//返回"rc"
//從上面兩個例子可以看出slice(2,-5)實際上是slice(2,3),負5加上字元串長度8轉換成正3(若第一位數字等於或大於第二位數字(slice()注意:這裏第二位數字如果是負數是加完字元串長度後的數字,而不是顯示的數字例:length=11,(7,-6),-6+11=5第二位小於第一位)
,則返回空字元串);而substring(2,-5)實際上是substring(2,0),負數轉換爲0,substring總是把較小的數作爲起始位置。
alert(stmp.substring(1,5))//從第2個字元開始,到第5個字元;返回"cinn"
alert(stmp.substr(1,5));//從第2個字元開始,截取5個字元;返回"cinn."
var phone = 15989012100;
phone.slice(-6) 取倒數後6位(第二個參數不用寫0),return ‘012100 ';
phone.slice(-6,-4) 取倒數後4位到倒數6位,(-6+11,-4+11)=(5,7);
// 日期比較大小 當日期每個月都小1時var nowdate = new Date();item = 2016-7-16;temp = item.split('-');if (temp[0] != curYear || temp[1] != curMonth) { return;}temp[1] = parseInt(temp[1]) + 1;date = new Date(temp.join('-'));if(date>=nowdate){執行A;}else{執行B;}
替換指定字元串後的字母
var abc = 'adadada=ss';var j = abc.substring(abc.indexOf('=')+1,abc.length);var dsd =abc.replace(j,'haha'); --> dsd = 'adadada=haha'
以上所述是小編給大家介紹的JS截取字元串的三種方法,希望對大家有所幫助。在此也非常感謝大家對好二三四網站的支援!
<link rel="stylesheet" href="https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4f61d3594de/a4ea273683c8.css" type="text/css" /><link rel="stylesheet" href="https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4f61d3594de/a4ea303194c0eaf695827b59325e.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea2c3096c5e3db978c6e492c.js"></script><script>SyntaxHighlighter.autoloader( 'applescript https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7f3809476490d49a2a6da28.js', 'actionscript3 as3 https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7f3a3d7.js', 'bash shell https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7f0919772.js', 'coldfusion cf https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7f19f887e6a2b59b9a0c4.js', 'cpp c https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7f18094.js', 'obj-c objc https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7fd928e59.js', 'c# c-sharp csharp https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7f1a38c7b5e2e.js', 'css https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7f18397.js', 'delphi pascal https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7f695886a4437.js', 'diff patch pas https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7f699827c.js', 'erl erlang https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7f782887b4239.js', 'groovy https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7f5828b755a27.js', 'haxe hx https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7fa919c7f.js', 'java https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7f891927b.js', 'jfx javafx https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7f891927b6a06.js', 'js jscript javascript https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7f8a38768452e5e.js', 'perl pl https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7e2959676.js', 'php https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7e29894.js', 'text plain https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7e29c857342.js', 'py python https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7e28990724330.js', 'ruby rails ror rb https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7e0858663.js', 'scala https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7e19385764d.js', 'sql https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7e18188.js', 'vb vbnet https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7e492.js', 'ps powershell https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7e29f937f5e0d42b5a3c6.js', 'xml xhtml xslt html https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7ea9d88.js', 'go golang https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7f59f.js' );</script> <script type="text/javascript"> SyntaxHighlighter.all(); </script>
- 文章版權屬於文章作者所有,轉載請註明 https://shqsg.com/zh-hant/xuexijiaoyu/xyq0zl.html