去除字符串中的所有-号
时间: 2024-09-18 08:12:34 浏览: 40
JS去掉字符串中所有的逗号
要去除字符串中的所有减号(-),你可以使用编程语言中的字符串操作函数或方法。这里提供几个常见编程语言的例子:
1. **Python**:
```python
def remove_dashes(input_string):
return input_string.replace("-", "")
input_str = "example-string-with-dashes"
new_str = remove_dashes(input_str)
print(new_str) # 输出: examplestringwithdashes
```
2. **JavaScript**:
```javascript
function removeDashes(inputStr) {
return inputStr.replace(/-/g, "");
}
let str = "example-string-with-dashes";
let newStr = removeDashes(str);
console.log(newStr); // 输出: example-string-with-dashes
```
3. **Java**:
```java
public String removeDashes(String input) {
return input.replaceAll("-", "");
}
String inputStr = "example-string-with-dashes";
String newStr = removeDashes(inputStr);
System.out.println(newStr); // 输出: example-string-with-dashes
```
4. **C#**:
```csharp
using System.Text.RegularExpressions;
public string RemoveDashes(string input)
{
return Regex.Replace(input, "-", "");
}
string inputStr = "example-string-with-dashes";
string newStr = RemoveDashes(inputStr);
Console.WriteLine(newStr); // 输出: example-string-with-dashes
```
阅读全文