用于修改字符串外引号的 ruby​​ 代码?

发布于 2024-10-07 03:28:45 字数 661 浏览 2 评论 0原文

有谁知道对字符串的外部引号进行操作的 Ruby gem(或内置的或本机语法)?

我发现自己一遍又一遍地编写这样的方法:

remove_outer_quotes_if_quoted( myString, chars ) -> aString
add_outer_quotes_unless_quoted( myString, char ) -> aString

第一个测试 myString 以查看其开始和结束字符是否与 chars 中的任何一个字符匹配。如果是,则返回删除了引号的字符串。否则它会原封不动地返回。 chars 默认为引号字符列表。

第二个测试 myString 以查看它是否已以 char 开头和结尾。如果是,则返回原样的字符串。如果不是,则返回前后添加了 char 的字符串,并且任何嵌入的 char 都会用反斜杠转义。 char 默认为默认字符列表中的第一个。

(当然,我手工拼凑的方法没有如此冗长的名称。)

我在公共存储库中查找了类似的方法,但找不到类似的方法。我是唯一一个需要做很多事情的人吗?如果没有,其他人是如何做到这一点的?

Does anyone know of a Ruby gem (or built-in, or native syntax, for that matter) that operates on the outer quote marks of strings?

I find myself writing methods like this over and over again:

remove_outer_quotes_if_quoted( myString, chars ) -> aString
add_outer_quotes_unless_quoted( myString, char ) -> aString

The first tests myString to see if its beginning and ending characters match any one character in chars. If so, it returns the string with quotes removed. Otherwise it returns it unchanged. chars defaults to a list of quote mark characters.

The second tests myString to see if it already begins and ends with char. If so, it returns the string unchanged. If not, it returns the string with char tacked on before and after, and any embedded occurrance of char is escaped with backslash. char defaults to the first in a default list of characters.

(My hand-cobbled methods don't have such verbose names, of course.)

I've looked around for similar methods in the public repos but can't find anything like this. Am I the only one that needs to do this alot? If not, how does everyone else do this?

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

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

发布评论

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

评论(3

寒江雪… 2024-10-14 03:28:45

如果你经常这样做,你可能想向 String 添加一个方法:

class String
  def strip_quotes
    gsub(/\A['"]+|['"]+\Z/, "")
  end
end

然后你可以调用 string.strip_quotes

添加引号是类似的:

class String
  def add_quotes
     %Q/"#{strip_quotes}"/ 
  end
end

这称为 string.add_quotes 并在添加双引号之前使用 strip_quotes。

If you do it a lot, you may want to add a method to String:

class String
  def strip_quotes
    gsub(/\A['"]+|['"]+\Z/, "")
  end
end

Then you can just call string.strip_quotes.

Adding quotes is similar:

class String
  def add_quotes
     %Q/"#{strip_quotes}"/ 
  end
end

This is called as string.add_quotes and uses strip_quotes before adding double quotes.

柠檬色的秋千 2024-10-14 03:28:45

这可能会解释如何删除和添加它们:

str1 = %["We're not in Kansas anymore."]
str2 = %['He said, "Time flies like an arrow, Fruit flies like a banana."']

puts str1
puts str2

puts

puts str1.sub(/\A['"]/, '').sub(/['"]\z/, '')
puts str2.sub(/\A['"]/, '').sub(/['"]\z/, '')

puts 

str3 = "foo"
str4 = 'bar'

[str1, str2, str3, str4].each do |str|
  puts (str[/\A['"]/] && str[/['"]\z/]) ? str : %Q{"#{str}"}
end

原来的两行:

# >> "We're not in Kansas anymore."
# >> 'He said, "Time flies like an arrow, Fruit flies like a banana."'

剥离引号:

# >> We're not in Kansas anymore.
# >> He said, "Time flies like an arrow, Fruit flies like a banana."

在需要时添加引号:

# >> "We're not in Kansas anymore."
# >> 'He said, "Time flies like an arrow, Fruit flies like a banana."'
# >> "foo"
# >> "bar"

This might 'splain how to remove and add them:

str1 = %["We're not in Kansas anymore."]
str2 = %['He said, "Time flies like an arrow, Fruit flies like a banana."']

puts str1
puts str2

puts

puts str1.sub(/\A['"]/, '').sub(/['"]\z/, '')
puts str2.sub(/\A['"]/, '').sub(/['"]\z/, '')

puts 

str3 = "foo"
str4 = 'bar'

[str1, str2, str3, str4].each do |str|
  puts (str[/\A['"]/] && str[/['"]\z/]) ? str : %Q{"#{str}"}
end

The original two lines:

# >> "We're not in Kansas anymore."
# >> 'He said, "Time flies like an arrow, Fruit flies like a banana."'

Stripping quotes:

# >> We're not in Kansas anymore.
# >> He said, "Time flies like an arrow, Fruit flies like a banana."

Adding quotes when needed:

# >> "We're not in Kansas anymore."
# >> 'He said, "Time flies like an arrow, Fruit flies like a banana."'
# >> "foo"
# >> "bar"
云巢 2024-10-14 03:28:45

如果 value[0] == value[-1] && 我会使用 value = value[1...-1] %w[' "].include?(value[0])。简而言之,这个简单的代码检查字符串的第一个和最后一个字符是否相同,如果它们是单/双引号,则将其删除。另外,可以根据需要添加许多报价类型

%w["adadasd" 'asdasdasd' 'asdasdasd"].each do |value|
  puts 'Original value: ' + value
  value = value[1...-1] if value[0] == value[-1] && %w[' "].include?(value[0])
  puts 'Processed value: ' + value
end

上面的示例将打印以下内容:

Original value: "adadasd"
Processed value: adadasd
Original value: 'asdasdasd'
Processed value: asdasdasd
Original value: 'asdasdasd"
Processed value: 'asdasdasd"

I would use the value = value[1...-1] if value[0] == value[-1] && %w[' "].include?(value[0]). In short, this simple code checks whether first and last char of string are the same and removes them if they are single/double quote. Additionally as many as needed quote types can be added.

%w["adadasd" 'asdasdasd' 'asdasdasd"].each do |value|
  puts 'Original value: ' + value
  value = value[1...-1] if value[0] == value[-1] && %w[' "].include?(value[0])
  puts 'Processed value: ' + value
end

The example above will print the following:

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