js截取字串 截取字串常用方法

來源:互聯網
上載者:User
1:explode 分割字串

eg:

$str = xxx:abc;

$abc = explode(':','$str'); //獲得的是一個數組

echo $abc[1]; //輸出abc

缺點:必須知道分割點,以及所需資料的位置

優點:好理解

2:str_replace 取代字串部分

eg:

$str = abcdefg;

$abc = str_replace('defg','','$str'); //將defg替換成空

echo $abc; //輸出abc

缺點:替換的部分必須已知

優點:好理解

3:substr 曆遍字串

eg:

$str = abcdefg;

$re = array();

for($i = 0;$i < strlen($str); $i++){ //將$str轉化成數組

$re[] = substr($str,$i,1);

}

echo $re[0].$re[1].$re[2]; //輸出abc

缺點:必須知道整個字串,效率低

優點:隨意拼接

以上就介紹了js截取字串 截取字串常用方法,包括了js截取字串方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.