隨筆-167  評論-65  文章-0  trackbacks-0

          ruby中有沒有像java中的String的trim那樣忽略空格的方法呢,答案當然是有的,查了下ruby API和google了下!

          lstrip : 去掉首空格

          rstrip : 去掉尾空格

          gsub : 去掉全部空格,不過要用到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) 評論(0)  編輯  收藏 所屬分類: Rails

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

          常用鏈接

          留言簿(6)

          隨筆分類(173)

          隨筆檔案(165)

          Links

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          已訪問數:
          free counters
          主站蜘蛛池模板: 白玉县| 丘北县| 庆安县| 视频| 廊坊市| 托克逊县| 雅江县| 华容县| 吉木萨尔县| 荔波县| 阿瓦提县| 全椒县| 遂昌县| 将乐县| 枣庄市| 十堰市| 蒙阴县| 宁都县| 台州市| 中超| 甘德县| 水富县| 平湖市| 柏乡县| 叙永县| 公主岭市| 康乐县| 剑阁县| 密云县| 双流县| 保靖县| 格尔木市| 沅江市| 拉萨市| 宜昌市| 彝良县| 浪卡子县| 天镇县| 正镶白旗| 石棉县| 永平县|