["1134","1135"] 如何将这个字符串的[]去掉 变成"1134","1135"
时间: 2023-08-16 13:08:56 浏览: 43
你可以使用字符串的 `replace` 方法来去掉字符串中的方括号。下面是一个示例代码:
```javascript
let str = '["1134","1135"]';
let result = str.replace(/[\[\]]/g, '');
console.log(result); // 输出 "1134","1135"
```
这里使用了正则表达式 `/[\[\]]/g` 匹配方括号,并使用空字符串替换它们,从而去掉了方括号。注意,正则表达式中的方括号需要使用转义字符 `\` 来进行转义。
这样就可以将 `["1134","1135"]` 的字符串形式转换为 `"1134","1135"` 的形式。
相关问题
怎样通过去除字符串两端的引号把一个字符串变成多个字符串
如果你想通过去除字符串两端的引号将一个字符串拆分成多个字符串,你可以使用字符串分割函数(splitting function)来实现。在C++中,可以使用`std::string`和`std::stringstream`来完成这个任务。以下是一个示例代码:
```cpp
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
std::vector<std::string> splitString(const std::string& str, char delimiter) {
std::vector<std::string> tokens;
std::string token;
std::stringstream ss(str);
while (std::getline(ss, token, delimiter)) {
tokens.push_back(token);
}
return tokens;
}
int main() {
std::string str = "\"Hello, World!\",\"Welcome\",\"to\",\"C++\"";
std::cout << "Original string: " << str << std::endl;
// 去除字符串两端的引号
std::string withoutQuotes = str.substr(1, str.size() - 2);
// 分割字符串
std::vector<std::string> splittedStrings = splitString(withoutQuotes, ',');
// 输出拆分后的字符串
for (const auto& s : splittedStrings) {
std::cout << "Splitted string: " << s << std::endl;
}
return 0;
}
```
在上述代码中,我们首先使用 `substr` 函数去除了字符串两端的引号,得到了 `withoutQuotes` 字符串。然后,我们使用 `splitString` 函数将 `withoutQuotes` 字符串按照逗号进行分割,得到了一个字符串的向量 `splittedStrings`。最后,我们通过遍历向量输出了拆分后的字符串。
输出结果将为:
```
Original string: "Hello, World!","Welcome","to","C++"
Splitted string: Hello, World!
Splitted string: Welcome
Splitted string: to
Splitted string: C++
```
希望这个例子能够帮助到你!如果你还有其他问题,请随时提问。
“[1,12,123,1234]”,将这个字符串变成list,java
在Java中,可以使用以下代码将字符串转换为列表:
```
String str = "[1,12,123,1234]";
str = str.substring(1, str.length() - 1); // 去掉括号
String[] strArr = str.split(",");
List<Integer> list = new ArrayList<>();
for (String s : strArr) {
list.add(Integer.parseInt(s.trim()));
}
```
首先,使用 `substring` 方法去掉字符串的开头和结尾的括号,得到纯数字字符串。然后,使用 `split` 方法将字符串按照逗号切分成字符串数组。接着,使用循环将字符串数组中的每个字符串转换为整数,并添加到 `ArrayList` 中。最后,得到一个包含所有数字的列表 `list`。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)