如果需要,如何使用 gsub 添加尾部斜杠

发布于 2024-09-14 09:06:19 字数 94 浏览 4 评论 0原文

如果需要,我正在尝试添加尾部斜杠:

a = '/var/www'
a.gsub
...

我不知道该怎么做。

I'm trying to add trailing slash if needed:

a = '/var/www'
a.gsub
...

I don't know how to do it.

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

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

发布评论

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

评论(8

居里长安 2024-09-21 09:06:19
a = File.join(a, "")

快速、简单,并且具有保证 a 以路径分隔符结尾的效果;也就是说,无论 a"/var/www" 还是 "/var/www/",它都会给出相同的结果。

这与上面 Joe White 的评论相同;我不知道他为什么不将其作为答案提交,因为它应该是一个答案。

奇怪的是,Pathname 库没有提供执行相同操作的便捷方法。

a = File.join(a, "")

Swift, simple, and has the effect of guaranteeing that a ends in a path separator; that is, it gives the same result whether a is "/var/www" or "/var/www/".

This is same as Joe White's comment above; I don't know why he didn't submit it as an answer, as it deserves to be one.

Oddly, the Pathname library doesn't provide a convenient way of doing the same thing.

墨落画卷 2024-09-21 09:06:19

这是一个更易读的版本

path << '/' unless path.end_with?('/')

Here's a bit more readable version

path << '/' unless path.end_with?('/')
冷月断魂刀 2024-09-21 09:06:19

为什么要使用gsub

  1. 您没有进行替换(不会从字符串中删除任何内容)
  2. 如果您正在替换,并且只需要在一处完成,请使用 sub 而不是 gsub
  3. 如果您要替换并想要修改字符串,请使用 sub!gsub!

由于您没有进行替换,因此只需在需要时附加斜杠:

path << '/' if path[-1] != '/' # Make sure path ends with a slash

更新: 要使其与旧版本的 Ruby (1.8.x) 兼容,请稍微修改它:

path << '/' if path[-1].chr != '/' # Make sure path ends with a slash

Why do you want to use gsub?

  1. You're not doing substitution (nothing is to be removed from the string)
  2. If you're substituting, and only need it done in one place, use sub not gsub.
  3. If you're substituting, and want to modify the string, use sub! or gsub!.

Since you're not doing substitution, just append the slash if needed:

path << '/' if path[-1] != '/' # Make sure path ends with a slash

Update: To make it compatible with older versions of Ruby (1.8.x), modify it slightly:

path << '/' if path[-1].chr != '/' # Make sure path ends with a slash
月亮邮递员 2024-09-21 09:06:19

您可以使用 .chomp('/') 删除可选的尾随 /,然后使用 .concat('/') 附加再次斜线。

'/var/www/'.chomp('/').concat('/')   # => "/var/www/"
'/var/www'.chomp('/').concat('/')    # => "/var/www/"

You can use .chomp('/') to strip of the optionally trailing / and then .concat('/') to append a slash again.

'/var/www/'.chomp('/').concat('/')   # => "/var/www/"
'/var/www'.chomp('/').concat('/')    # => "/var/www/"
动听の歌 2024-09-21 09:06:19

以下脚本显示了如何完成此操作:

a="/var/www";
print a + "\n";
a = a.gsub(/([^\/]$)/, '\1/');
print a + "\n";
a = a.gsub(/([^\/]$)/, '\1/');
print a + "\n";

它输出:

/var/www
/var/www/
/var/www/

并通过用相同的字符加上尾随的 //)来工作代码> 也是如此。

The following script shows how this can be done:

a="/var/www";
print a + "\n";
a = a.gsub(/([^\/]$)/, '\1/');
print a + "\n";
a = a.gsub(/([^\/]$)/, '\1/');
print a + "\n";

It outputs:

/var/www
/var/www/
/var/www/

and works by substituting the last character of the line (if it's not a /) with the same character plus a trailing / as well.

巨坚强 2024-09-21 09:06:19

已经介绍了几种不同的方法,通过另一种方法进行混合:

(a << '/').gsub!('//','/')

(a << '/').squeeze('/')

值得注意的是,这两种方法都会将字符串中任何位置的任何 '//' 情况转换为 '/' ,但如果您只是处理路径,这不太可能引起问题。

There are a couple of different ways that have already been covered, to through another into the mix:

(a << '/').gsub!('//','/')

or

(a << '/').squeeze('/')

It's worth noting though that both of those will convert any cases of '//' to '/' anywhere in the string, though if you are just dealing with paths, that's unlikely to cause problems.

来世叙缘 2024-09-21 09:06:19
"/var/www".gsub(/[^\/]$/, '\1/')  #=> "/var/www/"
"/var/www/".gsub(/[^\/]$/, '\1/') #=> "/var/www/"
"/var/www".gsub(/[^\/]$/, '\1/')  #=> "/var/www/"
"/var/www/".gsub(/[^\/]$/, '\1/') #=> "/var/www/"
万劫不复 2024-09-21 09:06:19

非常简单的 gsub,作为一个行比连接 (<<) 更容易,

a = '/var/www'
a.gsub('/?

这只是在字符串末尾找到一个可选的 / 字符并表示

, '/')

这只是在字符串末尾找到一个可选的 / 字符并表示

Pretty simple gsub, which is easier as a one liner than the concatenation (<<)

a = '/var/www'
a.gsub('/?

This simply finds an optional / character at the end of the string and rep

, '/')

This simply finds an optional / character at the end of the string and rep

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