开发者

Ruby string manipulation how to add space?

I am having problems string manipulation.

Here is to achieve with my string manipulation:

What I have:

<item name="RFSF "Blindspot"" type="project" id="34"/>

I want to add a line break just before the "" and it should also gsub all existing white space.

Here is the XML generator:

xmlmenu.item(:name=> convert_html_entities(kidsmovies[n].name),  :type=>"project", :id=> kidsmovies[n].id)      

Example want I want to do:

xmlmenu.item(:name=> convert_html_entities(kidsmovies[n].name).gs开发者_C百科ub(remove all whitespace).gsub(add one whitespace between the words).gsub.(create a linebreak just before ""),  :type=>"project", :id=> kidsmovies[n].id)  


First of all, you have a bad XML, it must be:

<item name="RFSF \"Blindspot\"" type="project" id="34"/>

then, to cut out extra spaces:

string.gsub(/\S+/).map.join(' ')

or:

string.split(' ').join(' ')

But what is create a linebreak just before ""?


Put this in a helper:

def format_name(str)
  html_escape(str.gsub(/\s+/, " ")) + "\n"
end

Then use it instead of convert_html_entities, which is not working (because it is not escaping quotes, making the XML output invalid). LIke so:

xmlmenu.item(:name=> format_name(kidsmovies[n].name), :type=>"project", :id=> kidsmovies[n].id)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜