16 12
发新话题
打印

ruby/rails中的中文处理(1)

ruby/rails中的中文处理(1)

由于当前的ruby并不支持unicode, 所以中文相关的处理在ruby中一直比较模糊,基本上是各自尝试的路子, 我也列出一些帮助自己整理思路

utf8和unicode的关系

utf其实是(Unicode Translation Format), 也就是unicode在传输时候的编码, 有utf8和utf16等多种编码, 其实uf8是一个对中文字很不友好的编码, 为什么呢,因为在utf8中, 一个中文是占用3个字节,而一个英文ascii字符却只占用1个字节, 这样一方面使得中文体积膨胀较厉害, 另外一方面也不能简单的通过计算字节数来推算字符数. utf16就比较正常, 每个字符占用两个字节.但是目前utf16已经后面的utf32应用较少, utf8 是比较公认的编码

utf8 还是 gb2312/gbk

我个人选择utf8,因为我觉得gb2312/gbk不是通用编码,如果使用可能还会有其他问题(比如多语言支持)

转换编码

我们需要require 'iconv' 库, 然后执行
复制内容到剪贴板
代码:
Iconv.conv("utf8","GBK","....") #把字符由GBK转换为UTF8
当源字符集中含有非法编码的时候,我们需要告诉iconv忽略, 否则iconv会报错停止
复制内容到剪贴板
代码:
Iconv.conv("utf8//IGNORE","GBK//IGNORE","....")
utf8 的中文操作:

计算字符数:

在默认的ruby环境下面, 需要执行
复制内容到剪贴板
代码:
require 'jcode'

$KCODE='u' # 或者$KCODE='UTF8' 两者是等价的
计算字符数:

使用jsize或者jlength
复制内容到剪贴板
代码:
"你好".size # 6

"你好".jsize # 2
截取字符操作
复制内容到剪贴板
代码:
"你好"[0,1] #乱码

"你好".scan(/./)[0,1].join # "你"
当然还有另外一个办法:

利用utf16编码每个字都是2个字节(头还有两个字节)的特点,我们可以先把字符转换为utf16,然后按照[0,n*2+2]的方法截取前n个字符 (试验成功.求证中)
复制内容到剪贴板
代码:
Iconv.conv("UTF8","UTF16",Iconv.conv("UTF16","UTF8","你好a中b文")[0,8]) #你好a
这世界总有你不明白

TOP

搬家公司为灾区人们祈祷

我相信,一定能挺过来的!@!@
      中国的人们都是很有勇气与毅力的!!
   

           不管遇到什么困难,只要我们团结一心,
          没有什么是办不到的!!
    加油!  中国!!!!!!!!!
搬家公司贴上对灾区人们的真诚祝福
┯┷┯┷┯┷┯┷┯┷┯┷┯┷┯┷┯┷┯┯┷┯┷┯┷┯
┷┯┷┯┷┯┷┯┷┯┷┯┷┯┷┯┷┯┷┯┷┯┷┯┷┯
┷┯┷┯┷┯上海搬家公司为灾区人们祈祷┷┯┷┯┷
┷┯┷┯┷┯┷┯┷┯┷┯┷┯┷┯┷┯┷┯┷┯┷┯┷┯
祝灾区人们平安幸福/搬家/物资回收

TOP

Former spokesman bashes Bush in new book

WASHINGTON -- In a shocking turnabout, the press secretary most known for defending President Bush on Iraq, Katrina and a host of other controversial issues produced a memoir damning of his old boss on nearly every level -- from too much secrecy to a less-than-honest selling of the war to a lack of personal candor and an unwillingness to admit mistakes.

In the first major insider account of the Bush White House, onetime spokesman Scott McClellan calls the operation "insular, secretive and combative" and says it veered irretrievably offcourse as a result.

wow gold
The White House responded angrily Wednesday to McClellan's confessional memoir, calling it self-serving sour grapes.

"Scott, we now know, is disgruntled about his experience at the White House," said current White House press secretary Dana Perino, a former deputy to McClellan. "We are puzzled. It is sad. This is not the Scott we knew."

McClellan was the White House press secretary from May 2003 to April 2006, the second of four so far in Bush's presidency.

He reveals that he was pushed to leave earlier than he had planned, and he displays some bitterness about that as well as about being sometimes kept out of the loop on key decision-making sessions.

He excludes himself from major involvement in some of what he calls the administration's biggest blunders, for instance the decision to go to war and the initial campaign to sell that decision to the American people. But he doesn't spare himself entirely, saying, "I fell far short of living up to the kind of public servant I wanted to be.

He includes criticism for the reporters whose questions he fielded. The news media, he says, were "complicit enablers" for focusing more on "covering the march to war instead of the necessity of war."

And McClellan issues this disclaimer about Bush: "I do not believe he or his White House deliberately or consciously sought to deceive the American people."

But most everything else he writes comes awfully close to making just this assertion, all the more stunning coming from someone who had been one of the longest-serving band of loyalists to come to Washington with Bush from Texas.

The heart of the book concerns Bush's decision to go to war in Iraq, a determination McClellan says the president had made by early 2002 -- at least a full year before the invasion -- if not even earlier.

"He signed off on a strategy for selling the war that was less than candid and honest," McClellan writes in "What Happened: Inside the Bush White House and Washington's Culture of Deception." The book is to go on sale June 1.

McClellan says Bush's main reason for war always was "an ambitious and idealistic post-9/11 vision of transforming the Middle East through the spread of freedom." But Bush and his advisers made "a marketing choice" to downplay this rationale in favor of one focused on increasingly trumped-up portrayals of the threat posed by the weapons of mass destruction.

During the "political propaganda campaign to sell the war to the American people," Bush and his team tried to make the "WMD threat and the Iraqi connection to terrorism appear just a little more certain, a little less questionable than they were." Something else was downplayed as well, McClellan says: any discussion of "the possible unpleasant consequences of war -- casualties, economic effects, geopolitical risks, diplomatic repercussions."

In Bush's second term, as news from Iraq grew worse, McClellan says the president was "insulated from the reality of events on the ground and consequently began falling into the trap of believing his own spin."

silkroad online gold
silkroad gold
All of this was a "serious strategic blunder" that sent Bush's presidency "terribly off course."

"The Iraq war was not necessary," McClellan concludes.

McClellan draws a portrait of Bush as possessing "personal charm, wit and enormous political skill." He says Bush's administration early on possessed "seeds of greatness."

world of warcraft gold
cheap wow gold
wow gold the full name is [url




TOP

实验箱

<a href="http://www.china-huaxia.cn/page/cp.asp?showid=151">霉菌交变试验箱 </a>
<a href="http://www.china-huaxia.cn/page/cp.asp?showid=157">紫外光耐气候试验箱</a>
<a href="http://www.china-huaxia.cn/page/cp.asp?showid=156">氙灯耐气候试验箱</a>
<a href="http://www.china-huaxia.cn/page/cp.asp?showid=148">防水试验箱</a>
<a href="http://www.china-huaxia.cn">淋雨试验箱</a>
专业从事网站优化,SEO,搜索引擎优化等专业网络技术

TOP

顶楼主

送你一个美女,顶你。



个性签名:
------------------------------------------------------------------------------------------------------------------------------
aoc power leveling,aoc powerleveling,AGE OF CONAN PowerLeveling,
AGE OF CONAN Power Leveling,world of warcraft power leveling

TOP

China not to sacrifice food for fuel: energy experts

China not to sacrifice food for fuel: energy experts
Updated: 2008-06-06 07:05
age of Conan gold
AOC gold
MANILA -- China has no plan to sacrifice food for fuel, the country's energy experts said on Thursday amid controversy over biofuel.

"Food security comes first in China, more important than fuel," said Song Yanqin, a co-drafter of China's national energy strategies, at Asia Clean Energy Forum 2008 sponsored by the Asian Development Bank (ADB) in Manila.
EVE ISK
"Biofuel," as said in the ADB's publication "Development Asia", has become a new buzz word all over the world, from the Philippines to Brazil, from the United States to the European Union.

Produced from agricultural crops such as maize, palm oil, sugarcane and jatropha, biofuel are used to run factories, power stations and vehicles. Countries that have the right conditions are setting aside millions of hectares of land for new plantations as international demand for prominent biofuel.
AOC gold
However, there is another side of the coin. The development of biofuel is considered as one reason for the global shortage of grain which drives food prices high in many countries. In the Philippines' southern region of Mindanao, for instance, rice prices have gone up to 50 pesos ($1.14) per kilogram.

"Biofuel is sensitive," said Song, especially in China, where 1. 3 billion people live on only 120 million hectares of arable land.
Age of Conan Gold
"Actually, in the global context, biofuel is still a controversial topic calling for serious study," said Zhou Dadi, an advisor to China's National Energy Leading Group.

At the same forum, Dan Millison, a US designer of higher-tech alcohol plants, said "food versus fuel is 99-percent noise."

"Do your homework and get a noise filter. Time magazine is not your key reference document," he addressed the audience via a video-conferencing system.  

"This problem is complicated," said Eric Usher, an official of United Nations Environment Program, adding that the dispute over biofuel will not be solved in a short time.

At the forum, with the theme of "Investing in Solutions that address Climate Change and Energy Security," Zhou Dadi explicated the sustainable energy strategy of China, giving priority to " conservation."

"Conservation comes first in the sustainable energy strategy of China," said the adviser, adding that specific and detailed regulations for energy conservation have been made in the country.

China has also set a target of 20 percent-intensity decrease for the 2006-2010 national development plan. That is, the country has to realize 4.4-percent energy-efficiency improvement annually in the five years, Zhou said.

Compared with the global annual figure of 1.2 percent over the past 30 years, the target for China is really impressive and encouraging, the adviser added.
wow gold the full name is [url




TOP

进口轴承 阜新进口轴承 阜新轴承

您写的真好 顶你了


进口轴承 阜新进口轴承 阜新轴承

TOP

对辊破碎机厂家 烘干机价格

选矿设备选铁设备选矿生产线球磨机价格报价

TOP

梅花糕机,煎饼机,面条机,巧克力喷泉机,科冷冰淇淋机www.126abc.com

梅花糕机,煎饼机,面条机,巧克力喷泉机,科冷冰淇淋机 www.126abc.com

梅花糕机       www.126abc.com
煎饼机         www.126abc.com
面条机         www.126abc.com
巧克力喷泉机   www.126abc.com
科冷冰淇淋机   www.126abc.com

TOP

Downpours threaten South China

Heavy rains are forecast in many parts of south China over the next nine days, and torrential rain, strong gales and thunderstorms are expected to strike some regions, the National Meteorological Center (NMC) warned Sunday.world of warcraft gold
Precipitation in Guizhou, Hubei, Hunan, Jiangxi and Guangxi is expected to be 30 to 70 percent more than, or even double, that of the same period in previous years, the NMC forecast.

Quake-affected areas of Sichuan province aren't expected to receive heavy rain but would face higher temperatures than in previous years, the NMC said.

Meteorological centers at various levels and of relevant departments, such as emergency response offices, are closely monitoring potential weather-related disasters, such as flashfloods and landslides, to provide early warnings and updated forecasts.
Final Fantasy XI Gil
The NMC on Saturday morning launched a Level 3 emergency response to the rainstorms anticipated in southern provinces.

The State Flood Control and Drought Relief Headquarters raised its emergency level on Friday from Level 3 to Level 2 because of the risk of rainstorm-triggered floods.

Torrential rains in the country's southern provinces last month killed 64 people.

And the forecasted rainstorms are expected to be severer than those of late May, deputy head of the headquarters Tian Yitang said.

"We should be on high alert and vigilant of the threat of floods, because the last round of downpours raised the water levels in some rivers," Tian said.

Damaged reservoirs in Sichuan had been ordered to maintain low water levels, because many of the reservoirs and irrigations systems in quake-affected areas haven't yet been repaired.
EVE ISK
Repairs would be finished on all reservoirs by June 20, the provincial bureau of water resources said.

Reinforcement work on the 722.6 km of river embankments damaged by the quake is expected to be completed by the month's end, the bureau said.

The heaviest rainstorms in 50 years drenched parts of Guangdong province over the weekend, killing at least one student and causing widespread flooding, the Guangzhou Daily reported yesterday.

One primary school student died after being swept away by a flood on Saturday in Maoming, in western Guangdong.
age of Conan gold
Another primary school student and a high school student were still missing.

More than 6,500 people had to be evacuated from their homes.

Torrential rains have affected the province over the past two weeks, with rainfall in some areas measuring more than 400 mm during the period.
Dofus Kamas
Rare torrential rain had also wreaked havoc on Hong Kong on Saturday morning, Xinhua reported.

The sky turned black, and rainstorms and thunderstorms caused schools and hospitals to close. Vehicles were submerged in water, and traffic was widely disrupted in the city.

Torrential rains on Friday also forced Shenzhen International Airport to cancel 12 flights and delay 150 more.

Thunderstorms in Yichang, Hubei province, caused blackouts in some parts of the city.

Agencies contributed to the story
wow gold the full name is [url




TOP

百度、google、雅虎

这三家搜索引擎可以说是中国互联网搜索引擎的三个大佬。然而他们的市场份额却差距相当的大。我做了seo这行后,就有一个梦想。那就是这三家搜索引擎可以平分天下。然而这是不可能的,在我们国内,关于权利方面,一直就没平衡过。也没有轮流坐庄一说,哎 ……
上海私家侦探
丽江自助游
一直不知道该写什么内容,那天在工作中遇到了一个google收录后又突然消失的问题,后来部门的一位seo专员解决了这个问题,我想和大家分享一下。同时在根据自己的一些经验说说我对这三家搜索引擎算法的了解。

云南丽江
丽江旅游

TOP

丽妍堂水上滚筒水上步行球派宁诸葛马

15058657179嘉鹏健身器材有限公司:是生产游艺,娱乐,健身,休闲用品专用生产商。产品有:|丽妍堂真轻松排汗机||妈咪乐|派宁蒸汽清洁机|水上步行球|水上滚筒|儿童手摇船游乐船|诸葛马|小洋人拉黄包车|夏娃之秀魔力挺|背背佳U9|水上蹦蹦床|水上跷跷板|水上双人划船|等20多种产品,欢迎新老客户来厂定购。详情网站: www.zgjp2008.com.地址:浙江永康市科技五金工业园。咨询订购电话:0579-87368579  QQ:976242168  893670131 1007100722

TOP

锦州SKF进口轴承 锦州SKF轴承 锦州进口轴承

TOP

私服一条龙

K歌去吧
走,去唱过歌了,不过还是不够过瘾,跑上来灌水





















专业承接传奇私服一条龙,提供传奇开区一条龙传奇私服一条龙 传奇世界私服一条龙,传奇私服商业服务端出售,网站制作,传奇私服广告代理.为想开传奇私服的朋友服务

TOP

主席青岛考察奥帆赛筹办 要求确保成功

该矿于2004年对600吨/制砖机日的一选厂进行二期扩建,先后投入2880万元,购置了选矿设备先进的破碎、球磨等选矿设备。 扩建完成投入运行以后,该厂生产能力迅速提升
生产5-2000T系调直机列选矿设备,30-2000T系列制砂设备,我公司的产品以质优、价廉,周到的售后服务,赢得了广大用户的信赖和好评。产品销售及售后服务网络遍布全国各地,远 ...
我们的选矿设备采矫直机用瑞典美卓公司的破碎机、圆锥磨机和中国矿业大学新研制的高效浮选柱设备,不但降低了能耗,也大大提高了自动化水平,同时利用两项已申报专利的新
选矿摇床是用于选别细粒箍筋机物料的重力选矿设备,广泛应用于锡、钨、金银、铅、锌、钽、铌、铁、锰、钛铁和煤等矿的选矿设备。我厂生产选矿摇床历史悠久,并不断开发创新

TOP

 16 12
发新话题