字符串操作
来源:互联网 发布:网络文学创作有前途吗 编辑:程序博客网 时间:2024/05/21 11:15
编程语言中大都有对字符串的操作的函数的封装,我们常用的一些操作有:判断是不是含有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
javapublic 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>"; //3echo strpos($a,',a')."<br>"; //3echo substr($a,2,4)."<br>"; //3,abecho str_replace(',','VV',$a)."<br>";//123VVabcprint_r(str_split($a,3));echo "<br>"; //123,abcecho strtoupper($a)."<br>"; //123,ABCecho strtolower("123.ABC")."<br>"; //123,abc
python
str="123,abc"print(str[2]) #3print(str.startswith('123'))#Trueprint(str.endswith('abc')) #Trueprint(str.index(',')) #3print(str[2:4]) #3,print(str.replace(',','VV'))#123VVabcprint(str.split(',')) #['123','abc']print(str.upper()) #123,ABCprint(str.upper().lower()) #123,abc
JavaScript
var str='123,abc';console.log(str[2]); //3console.log(str.includes(',')); //trueconsole.log(str.startsWith("123")); //trueconsole.log(str.endsWith("abc")); //trueconsole.log(str.indexOf(',')); //3console.log(str.lastIndexOf(',')); //3console.log(str.substr(2,4)); //3,abconsole.log(str.substring(2,4)); //3,console.log(str.replace(",","VV")); //123VVabcconsole.log(str.split(',')); //["123","abc"]console.log(" 123 ".trim()); //123console.log(str.toUpperCase()); //123,ABCconsole.log(str.toUpperCase().toLowerCase());//123,abcconsole.log(str.padEnd(9,"p")); //123.abcpp
0 0
- 字符串操作
- 字符串操作
- 操作字符串
- 字符串操作
- 字符串操作
- 字符串操作
- 字符串操作
- 字符串操作
- 字符串操作
- 字符串操作
- 字符串操作
- 字符串操作
- 字符串操作
- 字符串操作
- 字符串操作
- 字符串操作
- 字符串操作
- 字符串操作
- C程序语言中统计换行符'\n'例程的lastchar作用
- 获取Activity的返回参数
- opencv 直方图均衡化
- NYOJ 1057 寻找最大数(三)(贪心)
- C++对象模型-复制构造函数的构造操作
- 字符串操作
- 磁盘的分区与文件系统
- 使用 crontab实时mysql 的每天自动备份
- 关于指针int *p=&a和*p=&a的纠正
- HTML表单
- 数据的交换输出 HDU
- 无论是批八字还是起名
- 判断出栈入栈合法性
- HDU1150 Machine Schedule(最小顶点覆盖)