gpt4 book ai didi

ruby 字符串拆分问题

转载 作者:太空宇宙 更新时间:2023-11-03 17:40:12 25 4
gpt4 key购买 nike

我有这个字符串:"asdasda=asdaskdmasd&asmda=asdasmda&ACK=成功&asdmas=asdakmsd&asmda=adasda"

我想获取ACK和&符号之间的值,ACK和&符号之间的值可以改变...

谢谢

我想要 ruby​​ 的解决方案。

最佳答案

require "cgi"

query_string = "asdasda=asdaskdmasd&asmda=asdasmda&ACK=Success&asmda=asdakmsd"
parsed_query_string = CGI.parse(query_string)
#=> { "asdasda" => ["asdaskdmasd"],
# "asmda" => ["asdasmda", "asdakmsd"],
# "ACK" => ["Success"] }

parsed_query_string["ACK"].first
#=> "Success"

如果您还想重建查询字符串(尤其是与 URL 的其余部分一起),我建议您查看 addressable gem 。

require "addressable/uri"

# Note the leading '?'
query_string = "?asdasda=asdaskdmasd&asmda=asdasmda&ACK=Success&asmda=asdakmsd"
parsed_uri = Addressable::URI.parse(query_string)
parsed_uri.query_values["ACK"]
#=> "Success"

parsed_uri.query_values = parsed_uri.query_values.merge("ACK" => "Changed")
parsed_uri.to_s
#=> "?ACK=Changed&asdasda=asdaskdmasd&asmda=asdakmsd"
# Note how the order has changed and the duplicate key has been removed due to
# Addressable's built-in normalisation.

关于ruby 字符串拆分问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6017214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com