split()
trim()
indexOf()
replaceAll()
##ruby:
String.split
"hello world".split( " ")
returns [ "hello", "world" ].
String.strip
" hello world ".strip
returns "hello world".
String.index
"hello world".index( "w")
returns 6.
String.gsub(/\s/, ',')
"hello word".gsub(\/s\, ',')
returns "hello,word"
p.s.
sub() replace first
gsub() replace all
trim()
indexOf()
replaceAll()
##ruby:
String.split
"hello world".split( " ")
returns [ "hello", "world" ].
String.strip
" hello world ".strip
returns "hello world".
String.index
"hello world".index( "w")
returns 6.
String.gsub(/\s/, ',')
"hello word".gsub(\/s\, ',')
returns "hello,word"
p.s.
sub() replace first
gsub() replace all
本文介绍了几种常用的字符串处理方法,包括如何使用split()进行字符串拆分、trim()去除多余空格、indexOf()查找子串位置及replaceAll()替换字符等。通过具体的示例展示了这些方法的应用场景。
7242

被折叠的 条评论
为什么被折叠?



