编程语言中大都有对字符串的操作的函数的封装,我们常用的一些操作有:判断是不是含有xxx,把xx替换为yy,将字符串中逗号为分割分成数组等等。下面我们来简单罗列下这些函数:
功能 | Java | C# | Php | Python | JavaScript |
索引 | charAt | [] | [] | [] | []/charAt |
含有 | contains | Contains |
|
| includes |
首位 | startsWith endsWith | StartsWith EndsWith |
| startswith endswith | startsWith endWith |
字符(串)位置 | indexOf lastIndexOf | IndexOf LastIndexOf | strops strrpos | index | indexOf lastIndexOf |
截取 | substring | SubString | substr | [s:e] | substr/substring |
替换 | replace | Replace | str_replace preg_replace | replace | replace |
replaceAll | |||||
拆分 | split | Split | str_split | split | split |
修剪 | trim | Trim TrimEnd/TrimStart | trim |
| Trim trimLeft/trimRight |
大小写转换 | toUpperCase toLowerCase | ToUpperCase ToLowerCase | strtouppercase strtolowercase | lower upper | toUpperCase toLowerCase |
补全 |
| Pad PadLeft/PadRight | str_pad |
| padEnd padStart |
public static void main(String[] args){
String str="123,abc";
println(str.charAt(2)); //3
println(str.contains("3,a")); //true
println(str.startsWith("123")); //true
println(str.endsWith("abc")); //true
println(str.indexOf(",")); //3
println(str.substring(2,4)); //3,
println(str.replace(",","VV"));//123VVabc
println(Arrays.toString(str.split(",")));//[123,abc]
println(" 123 ".trim()); //123
println(str.toUpperCase()); //123,ABC
println(str.toUpperCase().toLowerCase());//123,abc
}
static void println(Object obj){
System.out.println(obj);
}
C#
static void Main(string[] args)
{
var str = "123,abc";
print(str[2]); //3
print(str.Contains(",")); //True
print(str.StartsWith("123")); //True
print(str.EndsWith("abc")); //True
print(str.IndexOf(",")); //3
print(str.Substring(2, 4)); //3,ab
print(str.Replace(",","VV")); //123VVabc
Array.ForEach(
str.Split(','),x=>print(x) //123 abc
);
print(" 123 ".Trim()); //123
print(str.ToUpper()); //123,ABC
print(str.ToUpper().ToLower()); //123,abc
print(str.PadRight(9, 'p')); //123,abcpp
Console.ReadKey();
}
static void print(Object obj) {
Console.WriteLine(obj);
}
php
$a='123,abc';
echo $a[2]."<br>"; //3
echo strpos($a,',a')."<br>"; //3
echo substr($a,2,4)."<br>"; //3,ab
echo str_replace(',','VV',$a)."<br>";//123VVabc
print_r(str_split($a,3));echo "<br>"; //123,abc
echo strtoupper($a)."<br>"; //123,ABC
echo strtolower("123.ABC")."<br>"; //123,abc
python
str="123,abc"
print(str[2]) #3
print(str.startswith('123'))#True
print(str.endswith('abc')) #True
print(str.index(',')) #3
print(str[2:4]) #3,
print(str.replace(',','VV'))#123VVabc
print(str.split(',')) #['123','abc']
print(str.upper()) #123,ABC
print(str.upper().lower()) #123,abc
JavaScript
var str='123,abc';
console.log(str[2]); //3
console.log(str.includes(',')); //true
console.log(str.startsWith("123")); //true
console.log(str.endsWith("abc")); //true
console.log(str.indexOf(',')); //3
console.log(str.lastIndexOf(',')); //3
console.log(str.substr(2,4)); //3,ab
console.log(str.substring(2,4)); //3,
console.log(str.replace(",","VV")); //123VVabc
console.log(str.split(',')); //["123","abc"]
console.log(" 123 ".trim()); //123
console.log(str.toUpperCase()); //123,ABC
console.log(str.toUpperCase().toLowerCase());//123,abc
console.log(str.padEnd(9,"p")); //123.abcpp