`
sitoto
  • 浏览: 120024 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

string.strip--去除字符串空格

阅读更多

ruby 字符串操作

 

1,切片:silce, [ ]-----------------[ ]是silce的别名,所以两者是完全相同的

操作1:判定字符串中是否含有字串/子模式

string[substring]

string[/pattern/]

string[/pattern/, position] #position之后的子串中是否含有/pattern/

如果存在返回子串/子模式串,否则返回nil

“hello world"["hello"]==="hello"

"hello world"[/.*lo/]==="hello"

"hello world"[/en/]===nil

 

操作2:使用索引截取子串

string[position] #注意返回的是ASCII码而不是字符

string[start, length]

string[start..end]

string[start...end]

 

2,比较:

== #比较字符串是否相等

eql? #??好像没有区别

<=> #用来比较字符串的大小,大于返回 1,小于返回 -1, 否则返回0

 

3,字符串的运算

downcase #改变字符串为全部小写

upcase #改变字符串为全部大写

swapcase#反写

capitalize #改变字符串为首字母大写

* #重复字符串

insert num, string #在num位置插入串string(insert没有!,因为insert会直接改变原串)

delete(!) string1 (,string2) #删除string1交string2的字符

gsub find, replace #将串中的find,替换为replace. find可以是正则表达式,replace很显然不可以。注意:是所有相同的都替换,相当与sed中的s/pattern/string/g

replace string #将字符串替换为string, 这是对象没有变,只是其中的内容发生了变化。

 

利用切片来改变字符串(silce!, [ ]=)

"hello"["ello"]= "w" # "hw"

"hello"[1]="wan" # "hwanllo"

“hello"[1..3]= "wrd" #"hwrdo"

"hello"[1...3]= "wr" #"hwrlo"

"hello"[1,3]="wrd" #"hwrdo"

"hello"[/el/]= "wr" #"hwrlo"

 

chomp(!) 用来摘除字符串末尾的换行符(如果不是换行符返回空)#注意只能是换行符,空格都不行

chop(!)用来摘除字符串末尾的最后一个字符

reverse(!)首尾倒置

split(/pattern/)将字符串分割成数组,分隔符是/pattern/(注意带!的方法不能用来改变类,所以split没有!)

 

 

字符串长度

string.length

string.size

 

字符串对齐

string.ljust num, char #用char来填充string,不足num的部分。注意左对齐是右填充。如果字符串长度比char大,忽略

string.rjust num, char

string.center num, char

 

string.lstring #trim字符串,去除左边空格

string.rstring

string.strip

..........那么如何去掉所有的空格呢? 很简单,使用gsub,进行替换

 

string.next/succ #string+1 不是+1这么简单。"a".next == "zz"

string1.upto(stringn) #string1, string2 ....stringn

 

字符串遍历:

string.each #分割不同项的必须是\n "hello\nworld".each {|e| puts e << ","}===

hello,

world,

"hello world".each{|e| puts e << ","}===

hello world,

string.each_byte #以字节为单位遍历

 

字符串到其他类型的转换

string.to_f/to_i #字符串--->浮点/整数

string.intern/to_sym #字符串--->符号

type.to_s #其他类型到字符串

Symbol.id2name#符号到字符串的专用转换

 

求字串的索引位置

string.index substring #正则表达式也可以

 

正则表达式专用

string.grep /pattern/ #如果不是正则表达式搜索不到任何东西,如果是且匹配返回包含整个字符串的一个数组

string =~ /pattern/ #pattern第一次出现的位置

string !~ /pattern/ #如果没有找到/pattern返回true(注意!)

分享到:
评论
1 楼 sitoto 2012-08-02  

If x is your column or vector:

sub(":", "\t", x)
See ?sub, which says

‘sub’ and ‘gsub’ perform replacement of the first and all matches respectively.

相关推荐

    strip-indent:删除字符串中每行的前导空格

    删除字符串中每行的前导空格 前导空格最少的行(忽略空行)确定要删除的数目。 对于删除多余的缩进很有用。 安装 $ npm install strip-indent 用法 import stripIndent from 'strip-indent' ; const string = '\...

    pythonstrip函数用法.docx

    pythonstrip函数用法 Python中的strip()函数是一个非常有用的字符串函数,它可以用来去除字符串中的空格和特定字符。在本文中,我们将深入探讨strip()函数的用法和功能。 strip()函数的语法如下: string.strip(...

    strip-indent-cli:删除字符串中每行的前导空格

    删除字符串中每行的前导空格 前导空格最少的行(忽略空行)确定要删除的数目。 安装 $ npm install --global strip-indent-cli 用法 $ strip-indent --help Usage $ strip-indent $ echo &lt;string&gt; | strip-...

    Python中使用strip()方法删除字符串中空格的教程

    strip()方法返回所有字符从开始及字符串的末尾(默认空格字符)被去除后的字符串的一个副本。 语法 以下是strip()方法的语法: str.strip([chars]); 参数 chars — 字符-从开始或结束的字符串被删除去除。 返回值 ...

    string类的常用方法.zip

    去除字符串两端的空白字符(包括空格、换行符、制表符等)。lstrip() 去除左侧的空白字符,rstrip() 去除右侧的空白字符。 4. split() 根据指定的分隔符将字符串分割成一个列表。 5. replace() 替换字符串中的...

    python 如何去除字符串头尾的多余符号

    在读文件时常常得到一些\n和引号之类的符号,可以使用字符串的成员函数strip()来去除。 1.去除首尾不需要的字符 a= 'This is test string' # strip()会默认去除'\n','\r','\t',' ',制表回车换行和空格等字符 a....

    zkeq#Coding#Python-字符串的strip函数1

    title: Python 字符串的strip函数字符串的strip函数功能string将去掉字符串左右两边的指定元素,默认是空格用法参数括弧里需要传一个你想去

    Python去除字符串两端空格的方法

    主要介绍了Python去除字符串两端空格的方法,本文主要讲解了string.lstrip、string.rstrip、string.strip等函数的运用,需要的朋友可以参考下

    Python备忘单

    Python备忘单 方便的备忘单。 字符串参考备忘单 在Python中,字符串可以做很多事情。...string.lstrip()/ string.rstrip()/ string.strip()返回没有左/右/左或右空格的字符串副本 string.count(substring)返回

    Python字符串函数strip()原理及用法详解

    strip:用于移除字符串头尾指定的字符(默认为空格)或字符序列。注意:该方法只能删除开头或是结尾的字符,不能删除中间部分的字符。 语法:str.strip([chars]) str = “*****this is **string** example….wow!!!*...

    在Python中操作字符串之rstrip()方法的使用

    rstrip()方法返回所有字符都被去除的字符串(缺省为空格字符)结束字符串的副本。 语法 以下是rstrip()方法的语法: str.rstrip([chars]) 参数 chars — 可以提供要去除的字符。 返回值 此方法返回的所有字符都被...

    python函数教程:Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)

    这篇文章主要介绍了Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等),需要的朋友可以参考下 去空格及特殊符号 s.strip().lstrip().rstrip(',') Python strip() 方法...

    浅析python 内置字符串处理函数的使用方法

    一、lower():将大写字母全部转为小写字母。如: 复制代码 代码如下:name=’G’b=name.lower() 二、title””:将字符串转化为标题,即所有单词...五、strip:返回去除两侧(不包括内部)空格的字符串 复制代码 代码如下:

    Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)

    Python strip() 方法用于移除字符串头尾指定的字符(默认为空格)。 复制字符串 #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sStr2 = sStr1 sStr1 = 'strcpy2' print sStr2 连接字符串 #strcat(sStr1,sStr2) sStr1 = '...

    浅谈Python3中strip()、lstrip()、rstrip()用法详解

    其中,strip()用于去除字符串的首尾字符,同理,lstrip()用于去除左边的字符,rstrip()用于去除右边的字符 Python中有三个去除头尾字符、空白符的函数,它们依次为: strip: 用来去除头尾字符、空白符(包括\n、\r、...

    Python rstrip()方法实例详解

    Python rstrip() 删除 string 字符串末尾的指定字符(默认为空格). 语法 rstrip()方法语法: str.rstrip([chars]) 参数 chars – 指定删除的字符(默认为空格) 返回值 返回删除 string 字符串末尾的指定字符后生成...

    PHP清除字符串中所有无用标签的方法

    很多时候需要输出一些 “纯” 字符串,也就是去除任何杂质,例如 Html 标签、空格之类的文本,输出的摘要就是如此,下面的这个函数可以帮你实现着一点. PHP实例代码如下: 复制代码 代码如下:function Bing_string_cleanr...

    strutil:简单的小助手功能,可互换使用字符串和正则表达式

    简单的辅助函数可以互换使用字符串和正则表达式(通常)。 要求 Python 3.4以上 模块内容 strutil.is_string(obj) 检查obj是否为字符串 strutil.is_regex(obj) 检查obj是否为正则表达式 strutil.replace(text, ...

    Smarty格式化数字为INT数

    Smarty格式化数字 default默认 escape转码 indent缩进 lower小写 nl2br换行符替换成 regex_replace正则替换 ...string_format 字符串格式化 strip 去除(多余空格) strip_tags 去除所有html标签 truncate 截取

    PHP100视频教程 28:PHP模板引擎Smarty的变量操作符.rar

    软件介绍 1、什么是Smarty变量操作符? ... 可用于操作变量,自定义函数和字符。...string_format[字符串格式化] strip[去除(多余空格)] strip_tags[去除html标签] truncate[截取] upper[大写] wordwrap[行宽约束]

Global site tag (gtag.js) - Google Analytics