段落首字母大写句子,如何改写成长尾?
- 内容介绍
- 文章标签
- 相关推荐
本文共计320个文字,预计阅读时间需要2分钟。
使用Ruby语言,我想将每个句子的第一个字母大写,并在每个句子的结尾处删除任何空格。以下是一个简单的实现:
rubyinput=this is the First Sentence. this is the Second Sentence.output=input.split('.').map { |sentence| sentence.strip.capitalize }.join('. ')puts output
输出结果:This is the First Sentence. This is the Second Sentence.
Input = "this is the First Sentence . this is the Second Sentence ." Output = "This is the First Sentence. This is the Second Sentence."
谢谢大家.
使用正则表达式(String#gsub):
Input = "this is the First Sentence . this is the Second Sentence ." Input.gsub(/[a-z][^.?!]*/) { |match| match[0].upcase + match[1..-1].rstrip } # => "This is the First Sentence. This is the Second Sentence." Input.gsub(/([a-z])([^.?!]*)/) { $1.upcase + $2.rstrip } # Using capturing group # => "This is the First Sentence. This is the Second Sentence."
我认为句子以.,?,!结尾.
UPDATE
input = "TESTest me is agreat. testme 5 is awesome" input.gsub(/([a-z])((?:[^.?!]|\.(?=[a-z]))*)/i) { $1.upcase + $2.rstrip } # => "TESTest me is agreat. Testme 5 is awesome" input = "I'm headed to stackoverflow.com" input.gsub(/([a-z])((?:[^.?!]|\.(?=[a-z]))*)/i) { $1.upcase + $2.rstrip } # => "I'm headed to stackoverflow.com"
本文共计320个文字,预计阅读时间需要2分钟。
使用Ruby语言,我想将每个句子的第一个字母大写,并在每个句子的结尾处删除任何空格。以下是一个简单的实现:
rubyinput=this is the First Sentence. this is the Second Sentence.output=input.split('.').map { |sentence| sentence.strip.capitalize }.join('. ')puts output
输出结果:This is the First Sentence. This is the Second Sentence.
Input = "this is the First Sentence . this is the Second Sentence ." Output = "This is the First Sentence. This is the Second Sentence."
谢谢大家.
使用正则表达式(String#gsub):
Input = "this is the First Sentence . this is the Second Sentence ." Input.gsub(/[a-z][^.?!]*/) { |match| match[0].upcase + match[1..-1].rstrip } # => "This is the First Sentence. This is the Second Sentence." Input.gsub(/([a-z])([^.?!]*)/) { $1.upcase + $2.rstrip } # Using capturing group # => "This is the First Sentence. This is the Second Sentence."
我认为句子以.,?,!结尾.
UPDATE
input = "TESTest me is agreat. testme 5 is awesome" input.gsub(/([a-z])((?:[^.?!]|\.(?=[a-z]))*)/i) { $1.upcase + $2.rstrip } # => "TESTest me is agreat. Testme 5 is awesome" input = "I'm headed to stackoverflow.com" input.gsub(/([a-z])((?:[^.?!]|\.(?=[a-z]))*)/i) { $1.upcase + $2.rstrip } # => "I'm headed to stackoverflow.com"

