字符串中的反斜杠返回两个反斜杠

发布于 2024-11-25 11:39:33 字数 349 浏览 0 评论 0 原文

输入了此访问令牌值

864876322670016\u00257C4e1d481ecad9eb45b9386745.1-1026038548\u00257CshuA8v7lgo7-hRr2AjbUBd3shek,

我在表单上

但它返回时带有双反斜杠,如下所示864876322670016\\u00257C4e1d481ecad9eb45b9386745.1-1026038548\\u00257CshuA8v7lgo7-hRr2AjbUBd3shek

我将此值传递给 Facebook GraphAPI,这会返回错误。

如何获得 return 将双反斜杠替换为单反斜杠?或者有没有办法让双反斜杠不出现?

I entered this access token value

864876322670016\u00257C4e1d481ecad9eb45b9386745.1-1026038548\u00257CshuA8v7lgo7-hRr2AjbUBd3shek

on a form but it was returned with double backslash like this

864876322670016\\u00257C4e1d481ecad9eb45b9386745.1-1026038548\\u00257CshuA8v7lgo7-hRr2AjbUBd3shek

I'm passing this value to Facebook GraphAPI and this returns an error.

How can I get return replace the double backslash with a single one? or is there a way for the double backslash to not appear?

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

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

发布评论

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

评论(2

柳絮泡泡 2024-12-02 11:39:34

您确定它实际上是用双反斜杠返回的吗?内部带有反斜杠的字符串看起来像有双反斜杠,因为 Ruby 会转义它们:

> a = 'aaa\bbb\ccc'
=> "aaa\\bbb\\ccc"           # Looks like doubles
> a
=> "aaa\\bbb\\ccc"
> a.inspect
=> "\"aaa\\\\bbb\\\\ccc\""   # Looks even worse
> puts a
aaa\bbb\ccc                  # ...but it isn't

但是如果它们是双反斜杠,你可以这样做:

> puts aa
aaa\\bbb\\ccc                # String with double backslash
> aa.gsub!("\\\\", "\\")
> puts aa
aaa\bbb\ccc

Are you sure it's actually returned with double backslashes? Internally strings with backslashes will look like they have double backslashes because Ruby is escaping them:

> a = 'aaa\bbb\ccc'
=> "aaa\\bbb\\ccc"           # Looks like doubles
> a
=> "aaa\\bbb\\ccc"
> a.inspect
=> "\"aaa\\\\bbb\\\\ccc\""   # Looks even worse
> puts a
aaa\bbb\ccc                  # ...but it isn't

But if they are double backslashes you can do something like this:

> puts aa
aaa\\bbb\\ccc                # String with double backslash
> aa.gsub!("\\\\", "\\")
> puts aa
aaa\bbb\ccc
轮廓§ 2024-12-02 11:39:34

这只是它的显示方式,以转义形式。您的错误可能在其他地方。

这是对输出的常见误解,当您第一次看到它时会有点困惑,正如 Casper 所指出的那样。

来自这个问题/答案,该人的问题本质上是相同的:

该死。我忘记了,当结果显示在双引号中时,它表明它已转义。

还有一个关于这个感知问题的简短讨论在此博文中

It's just the way it's being displayed, in escaped form. Your error is likely elsewhere.

This is a common misinterpretation of the output, and a little confusing when you first see it, as Casper has pointed out.

From this question/answer, where the person's issue was essentially the same:

Dang it. I forgot that when the result is displayed in double quotes it shows it escaped.

There's also a short discussion of this perceived issue in this blog post.

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