C++接收字符串数组_PHP常用字符串函数(1),PHP面试重点
歡迎訪問
獲取字符串長度
1、strlen ( string $string ) : int
獲取字符串長度
$str = 'abc';echo strlen($str);//輸出3$str = 'a bc';echo strlen($str);//輸出4,空格也算一個字符$str = "abc";echo strlen($str);//輸出4,空格也算一個字符,是轉義字符,算一個字符$str = "abc";echo strlen($str);//輸出5,和都是轉義字符,都是一個字符,$str = "abc";echo strlen($str);//輸出5,換行是,和都是轉義字符,都是一個字符,$str = "極客時光機";echo strlen($str);//輸出15,在utf-8下,一個漢字長度算3,ANSI格式下顯示22、mb_strlen ( string $str [, string $encoding = mb_internal_encoding() ] ) : mixed
參數說明:
str要檢查長度的字符串。
encodingencoding 參數為字符編碼。如果省略,則使用內部字符編碼。
$str = "極客時光機";echo mb_strlen($str);//15字符串查找函數
1、strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) : int
查找字符串首次出現的位置,返回 needle 在 haystack 中首次出現的數字位置。haystack是草垛,needle是針,就是在草垛中尋找針。
參數說明:
haystack在該字符串中進行查找。
needle如果 needle 不是一個字符串,那么它將被轉換為整型并被視為字符的順序值。
offset如果提供了此參數,搜索會從字符串該字符數的起始位置開始統計。 如果是負數,搜索會從字符串結尾指定字符數開始。
返回值:返回 needle 存在于 haystack 字符串起始的位置(獨立于 offset)。同時注意字符串位置是從0開始,而不是從1開始的。如果沒找到 needle,將返回 FALSE。
2、stripos ( string $haystack , string $needle [, int $offset = 0 ] ) : int
和strpos()唯一不同的是忽略大小寫。函數名稱多了個“i”,即ignore。
3、strrpos ( string $haystack , string $needle [, int $offset = 0 ] ) : int
計算指定字符串在目標字符串中最后一次出現的位置。
4、strripos ( string $haystack , string $needle [, int $offset = 0 ] ) : int
計算指定字符串在目標字符串中最后一次出現的位置(不區分大小寫),和strrpos()不同
5、strstr ( string $haystack , mixed $needle [, bool $before_needle = FALSE ] ) : string
返回從 needle 第一次出現的位置開始到 haystack 結尾的字符串。
參數說明:
haystack輸入字符串。
needle如果 needle 不是一個字符串,那么它將被轉化為整型并且作為字符的序號來使用。
before_needle若為 TRUE,strstr() 將返回 needle 在 haystack 中的位置之前的部分。
返回值:返回字符串的一部分,沒有要查找的字符串則返回 FALSE。
6、strchr
strstr()函數的別名。
7、stristr ( string $haystack , mixed $needle [, bool $before_needle = FALSE ] ) : string
和strstr()不同的是,該函數忽略大小寫。
8、strrchr ( string $haystack , mixed $needle ) : string
查找指定字符在字符串中的最后一次出現。該函數返回從needle 的最后出現位置開始,到 haystack 末尾。
返回值:該函數返回字符串的一部分。如果 needle 未被找到,返回 FALSE。
字符串替換函數(查找并替換)
1、str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) : mixed
在subject中查找search,全部用replace替換,返回替換后的字符串。
參數說明:
如果 search 和 replace 為數組,那么 str_replace() 將對 subject 做二者的映射替換。如果 replace 的值的個數少于 search 的個數,多余的替換將使用空字符串來進行。如果 search 是一個數組而 replace 是一個字符串,那么 search 中每個元素的替換將始終使用這個字符串。該轉換不會改變大小寫。
如果 search 和 replace 都是數組,它們的值將會被依次處理。
search查找的目標值,也就是 needle。一個數組可以指定多個目標。
replacesearch 的替換值。一個數組可以被用來指定多重替換。
subject執行替換的數組或者字符串。也就是 haystack。
如果 subject 是一個數組,替換操作將遍歷整個 subject,返回值也將是一個數組。
count如果被指定,它的值將被設置為替換發生的次數。
返回值:返回替換后的數組或者字符串。
2、str_ireplace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) : mixed
和str_replace()不同的是,該函數忽略大小寫。
字符串大小寫轉換函數
1、strtolower ( string $string ) : string
將 string 中所有的字母字符轉換為小寫并返回。
2、strtoupper ( string $string ) : string
將 string 中所有的字母字符轉換為大寫并返回。
3、ucwords ( string $str [, string $delimiters = ” fv” ] ) : string
將字符串中每個單詞的首字母轉換為大寫。
str輸入字符串。
delimiters可選的,單詞分割字符,默認分隔符為空格符。
4、ucfirst ( string $str ) : string
將字符串的首字母轉換為大寫,返回轉換后的字符串。
5、lcfirst ( string $str ) : string
將字符串的首字母轉換為小寫,返回轉換后的字符串。
字符串截取函數
1、substr ( string $string , int $start [, int $length ] ) : string
截取字符串,返回截取后的字符串。
參數說明:
(1)string輸入字符串。必須至少有一個字符。
(2)start如果 start 是非負數,返回的字符串將從 string 的 start 位置開始,從 0 開始計算。
如果 start 是負數,返回的字符串將從 string 結尾處向前數第 start 個字符開始。
如果 string 的長度小于 start,將返回 FALSE。
(3)length如果提供了正數的 length,返回的字符串將從 start 處開始最多包括 length 個字符(取決于 string 的長度)。
如果提供了負數的 length,那么 string 末尾處的 length 個字符將會被省略(若 start 是負數則從字符串尾部算起)。如果 start 不在這段文本中,那么將返回 FALSE。
如果提供了值為 0,FALSE 或 NULL 的 length,那么將返回一個空字符串。
如果沒有提供 length,返回的子字符串將從 start 位置開始直到字符串結尾。
返回值:返回提取的子字符串, 或者在失敗時返回 FALSE。
2、substr_replace ( mixed $string , mixed $replacement , mixed $start [, mixed $length ] ) : mixed
替換字符串的子字符串。將字符串從start位置開始的子字符串替換為replacement。
(1)string輸入字符串。
(2)replacement替換的字符串。
(3)start如果 start 為正數,替換將從 string 的 start 位置開始。
如果 start 為負數,替換將從 string 的倒數第 start 個位置開始。
(4)length如果設定了這個參數并且為正數,表示 string 中被替換的子字符串的長度。如果設定為負數,它表示待替換的子字符串結尾處距離 string 末端的字符個數。如果沒有提供此參數,那么它默認為 strlen( string ) (字符串的長度)。當然,如果 length 為 0,那么這個函數的功能為將 replacement 插入到 string 的 start 位置處。
返回值:返回結果字符串。如果 string 是個數組,那么也將返回一個數組。
3、mb_substr ( string $str , int $start [, int $length = NULL [, string $encoding = mb_internal_encoding() ]] ) : string
根據字符數執行一個多字節安全的 substr() 操作。
參數說明:
(1)str從該 string 中提取子字符串。
(2)start如果 start 不是負數,返回的字符串會從 str 第 start 的位置開始,從 0 開始計數。
如果 start 是負數,返回的字符串是從 str 末尾處第 start 個字符開始的。
lengthstr 中要使用的最大字符數。如果省略了此參數或者傳入了 NULL,則會提取到字符串的尾部。
encodingencoding 參數為字符編碼。如果省略,則使用內部字符編碼。
返回值:返回子字符串。
總結
以上是生活随笔為你收集整理的C++接收字符串数组_PHP常用字符串函数(1),PHP面试重点的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: bit 位图
- 下一篇: 307 跳转会携带请求方法吗_面试官:G