浏览 1974 次
|
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
时间:2006-12-12
中文字符串,如何Substr呢?我发现直接用会乱码
如果是英文是没有问题的,如果是中文,最后的几位是会乱码的。 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
时间:2006-12-12
在其它地方看到的:
在config/environment.rb里加上一行:$KCODE = 'utf8' 或则在启动的时候加上ruby.exe -KU script/server 然后就可以用split方法把string转成char来进行截取 如<%= @title.split(//)[0,10] %> #截取title字段的前10个字符 |
|
| 返回顶楼 | |
|
时间:2006-12-12
谢谢!
|
|
| 返回顶楼 | |
|
时间:2006-12-13
|
|
| 返回顶楼 | |
|
时间:2007-02-09
# Be sure to restart your web server when you modify this file.
# Uncomment below to force Rails into production mode when # you don't control web/app server and can't set it the proper way # ENV['RAILS_ENV'] ||= 'production' # 我在这里已经加入了 $KCODE = 'utf8' # Specifies gem version of Rails to use when vendor/rails is not present RAILS_GEM_VERSION = '1.2.1' unless defined? RAILS_GEM_VERSION 在rhtml中这样写 <%= product.split(//)[0,10] %> 没想到居然会出错. 我现在的解决办法是在 application_helper.rb 文件中加入了 truncate 方法,不过听说速度会慢 module ApplicationHelper def truncate(text, length = 30, truncate_string = "...") if text.nil? then return end chars = text.split(//) if chars.length > length chars[0..(length-3)].join + truncate_string else text end end end |
|
| 返回顶楼 | |
|
时间:2007-02-09
如果只是用于html显示,可以用js截取。
假设长度为1000的字符串,你想截取长度为10,那么先用ruby截取20,然后把它抛给js截取10即可。 ^_^ |
|
| 返回顶楼 | |
|
时间:2007-02-09
如果是Rails 1.2.1,可以用chars。
|
|
| 返回顶楼 | |







