如何拆分字符串但包含一些符号(#'s和+'s)

How to split a string but include some symbols (#'s and +'s)

本文关键字:符号 包含一 何拆分 拆分 字符串      更新时间:2023-09-26

我想用从stackoverflow API中提取的标签创建页面,但我很难使用c#和c++标签,因为它只提取字母c。有什么方法可以在我的拆分方法中包含这两个字符吗?

var tag = location.search.split('tag=')[1]

以及重定向功能

    function reDirect()
  {
  d = d3.select(this).node().__data__;
  window.location.assign("details.html?tag="+d.name);  
  }
正如其他人所指出的,您的代码是正确的。您面临的问题是您在url中使用了字符#。浏览器将这些字符解释为片段标识符。

为了能够在url中使用字符#,您应该对存储在"标记"查询变量中的值进行编码。

在url中存储值:

window.location.assign("details.html?tag=" + encodeURIComponent(d.name));

从url检索值:

var tag = decodeURIComponent(location.search.split('tag=')[1]);