隨筆-167  評(píng)論-65  文章-0  trackbacks-0

          ruby中有沒(méi)有像java中的String的trim那樣忽略空格的方法呢,答案當(dāng)然是有的,查了下ruby API和google了下!

          lstrip : 去掉首空格

          rstrip : 去掉尾空格

          gsub : 去掉全部空格,不過(guò)要用到pattern匹配

          eg:

          s1 = " Test  whitespace"
          s2 = " hello  Ruby  Rails  "
          s3 = "trailing  "
          puts s1.lstrip+s3 # show  lstrip remove leading  whitespace
          puts s2.rstrip+s3 # show  rstrip  remove trail whitespace
          puts s1.strip + s2.strip + s3 # show strip remove leading and trail whitespace
          puts s2.gsub(/\s+/,'')
          puts s2.gsub(' ','')
          puts s2.gsub!(' ','')
          puts s2.gsub(//,'') # four methods show remove all whitespace

           

          #ref : http://railsforum.com/viewtopic.php?id=15030
          #ref : http://www.nabble.com/remove-all-whitespaces-in-a-string-td10835684.html#a10835684



          write by feng
          posted on 2009-04-02 16:03 fl1429 閱讀(6330) 評(píng)論(0)  編輯  收藏 所屬分類: Rails

          <2009年4月>
          2930311234
          567891011
          12131415161718
          19202122232425
          262728293012
          3456789

          常用鏈接

          留言簿(6)

          隨筆分類(173)

          隨筆檔案(165)

          Links

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          已訪問(wèn)數(shù):
          free counters
          主站蜘蛛池模板: 文山县| 天长市| 永兴县| 扎鲁特旗| 日照市| 玛纳斯县| 通海县| 邯郸县| 洪雅县| 宜川县| 池州市| 巴南区| 大连市| 达拉特旗| 阿荣旗| 金华市| 普陀区| 荔浦县| 平度市| 南宁市| 通化县| 民和| 开阳县| 株洲县| 牙克石市| 噶尔县| 高淳县| 璧山县| 晋中市| 崇左市| 柏乡县| 泸溪县| 拜城县| 田林县| 彭水| 辛集市| 文登市| 蕲春县| 北海市| 定安县| 九寨沟县|