仅删除字符串开头的空白字符

发布于 2024-11-03 20:53:08 字数 840 浏览 1 评论 0原文

编辑:我通过使用 strip! 解决了这个问题 删除前导和尾随空格正如我在该视频中展示的那样。然后,我通过迭代并添加空格来恢复数组每个字符串末尾的空格。

这个问题与“欺骗”不同,因为我的目的是保留末尾的空格。然而,脱衣!如果您愿意的话,将删除前导和尾随空格。

我有一个单词数组,我试图删除单词开头可能存在的任何空格,而不是结尾处的空格。 rstrip!只处理字符串的结尾。我想从字符串的开头删除空格。

example_array = ['peanut', ' butter', 'sammiches ']
desired_output = ['peanut', 'butter', 'sammiches ']

正如您所看到的,并非数组中的所有元素都存在空格问题,因此我不能像所有元素都以单个空格字符开头那样删除第一个字符。

完整代码:

words = params[:word].gsub("\n", ",").delete("\r").split(",")
words.delete_if {|x| x == ""}
words.each do |e|
  e.lstrip!
end

用户可以在表单上输入的示例文本:

Corn on the cob,
Fibonacci,
StackOverflow
Chat, Meta, About
Badges 
Tags,,
Unanswered
Ask Question

Edit: I solved this by using strip! to remove leading and trailing whitespaces as I show in this video. Then, I followed up by restoring the white space at the end of each string the array by iterating through and adding whitespace.

This problem varies from the "dupe" as my intent is to keep the whitespace at the end. However, strip! will remove both the leading and trailing whitespace if that is your intent.

I have an array of words where I am trying to remove any whitespace that may exist at the beginning of the word and not ones at the end. rstrip! just takes care of the end of a string. I want whitespaces removed from the beginning of a string.

example_array = ['peanut', ' butter', 'sammiches ']
desired_output = ['peanut', 'butter', 'sammiches ']

As you can see, not all elements in the array have the whitespace problem, so I can't just delete the first character as I would if all elements started with a single whitespace character.

Full code:

words = params[:word].gsub("\n", ",").delete("\r").split(",")
words.delete_if {|x| x == ""}
words.each do |e|
  e.lstrip!
end

Sample text that a user may enter on the form:

Corn on the cob,
Fibonacci,
StackOverflow
Chat, Meta, About
Badges 
Tags,,
Unanswered
Ask Question

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

东走西顾 2024-11-10 20:53:08

String#lstrip(或String#lstrip!)是你在追求什么

desired_output = example_array.map(&:lstrip)

关于您的代码的更多评论:

  1. delete_if {|x| x == ""} 可以替换为 delete_if(&:empty?)
  2. 除非您想要 reject! 因为 delete_if只会返回一个不同的数组,而不是修改现有的数组。
  3. words.each {|e| e.lstrip!} 可以替换为 words.each(&:lstrip!)
  4. delete("\r") 应该是多余的,如果你'在 Windows 机器上读取 Windows 风格的文本文档,或在 Unix 机器上阅读 Unix 风格的文档
  5. split(",") 可以替换为 split(", ")< /code> 或 split(/, */) (或 /, ?/ 如果最多有一个空格)

所以现在看起来像:

words = params[:word].gsub("\n", ",").split(/, ?/)
words.reject!(&:empty?)
words.each(&:lstrip!)

我会如果您有可用的示例文本,能够提供更多建议。

编辑:好的,这里是:

temp_array = text.split("\n").map do |line|
  fields = line.split(/, */)
  non_empty_fields = fields.reject(&:empty?)
end
temp_array.flatten(1)

使用的方法是String#splitEnumerable#mapEnumerable#rejectArray#flatten

Ruby 也有用于解析逗号分隔文件的库,但我认为它们在 1.8 和 1.9 之间有点不同。

String#lstrip (or String#lstrip!) is what you're after.

desired_output = example_array.map(&:lstrip)

More comments about your code:

  1. delete_if {|x| x == ""} can be replaced with delete_if(&:empty?)
  2. Except you want reject! because delete_if will only return a different array, rather than modify the existing one.
  3. words.each {|e| e.lstrip!} can be replaced with words.each(&:lstrip!)
  4. delete("\r") should be redundant if you're reading a windows-style text document on a Windows machine, or a Unix-style document on a Unix machine
  5. split(",") can be replaced with split(", ") or split(/, */) (or /, ?/ if there should be at most one space)

So now it looks like:

words = params[:word].gsub("\n", ",").split(/, ?/)
words.reject!(&:empty?)
words.each(&:lstrip!)

I'd be able to give more advice if you had the sample text available.

Edit: Ok, here goes:

temp_array = text.split("\n").map do |line|
  fields = line.split(/, */)
  non_empty_fields = fields.reject(&:empty?)
end
temp_array.flatten(1)

The methods used are String#split, Enumerable#map, Enumerable#reject and Array#flatten.

Ruby also has libraries for parsing comma seperated files, but I think they're a little different between 1.8 and 1.9.

紫竹語嫣☆ 2024-11-10 20:53:08
> ' string '.lstrip.chop
=> "string"

去掉两个空格...

> ' string '.lstrip.chop
=> "string"

Strips both white spaces...

惯饮孤独 2024-11-10 20:53:08

编辑:添加了 gsub 替代方案。

为什么不将 gsub 与正则表达式一起使用:

' Hello there '.gsub(/^\s/, '')   #=> "Hello there " 

这会将输入开头的空格替换为空字符串。有关详细信息,请参阅 https://apidock.com/ruby/String/gsub

code>strip 方法很优雅,但它删除了字符串开头和结尾的空格。

' Hello there '.strip   #=> "Hello there"

有关信息,请参阅 https://apidock.com/ruby/String/strip

EDIT: a gsub alternative added.

Why not use gsub with regex:

' Hello there '.gsub(/^\s/, '')   #=> "Hello there " 

This will replace whitespace at the beginning of the input with an empty string. For more info, see https://apidock.com/ruby/String/gsub

The strip method is elegant but it removes whitespace from both the beginning and end of string.

' Hello there '.strip   #=> "Hello there"

For info, see https://apidock.com/ruby/String/strip

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文