将所有特殊字符替换为连字符并删除所有空格。

huangapple go评论68阅读模式
英文:

How to replace all special characters with hyphen and also remove all spaces?

问题

"我想要在特定字符串中移除所有特殊字符并替换为连字符。所有空格需要移除,并转换为小写。是否可以用一行代码完成?

一个例子:"HELLO - DI&RIC" 应该变成:"hello-di-ric""

英文:

I want to remove all special characters in a certain string and replace them with hyphen. All spaces need to be remove and also lowercase. is it possible to do it with 1 line?

An example: "HELLO - DI&RIC" -> should become: "hello-di-ric"

答案1

得分: 2

以下JAVA代码段应该正常工作:

String str = "HELLO - DI&RIC";
String noSpaceString = str.toLowerCase().replaceAll("\\s", "");
String alphabetsOnly = noSpaceString.replaceAll("[^a-zA-Z]+", "-");
String alphabetsAndDigitsOnly = noSpaceString.replaceAll("[^a-zA-Z0-9]+", "-");

为了提高可读性,我使用了额外的变量。您可以将它们移除,将代码变为一行,如下所示:

String str = "HELLO - DI&RIC";
String alphabetsAndDigitsOnly = str.toLowerCase().replaceAll("\\s", "").replaceAll("[^a-zA-Z0-9]+", "-");
英文:

Following JAVA code snippet should work fine :

String str = "HELLO - DI&RIC";
String noSpaceString = str.toLowerCase().replaceAll("\\s", "");
String alphabetsOnly = noSpaceString.replaceAll("[^a-zA-Z]+", "-");
String alphabetsAndDigitsOnly = noSpaceString.replaceAll("[^a-zA-Z0-9]+", "-");

I have used extra variables for readability purposes. You can remove them to make it a one-line code, like following :

String str = "HELLO - DI&RIC";
String alphabetsAndDigitsOnly = str.toLowerCase().replaceAll("\\s", "").replaceAll("[^a-zA-Z0-9]+", "-");

答案2

得分: 0

You should look into the replaceAll() method.
它接受正则表达式和替换字符串。
我可能首先调用toLowerCase(),然后用于分隔特殊字符的空格,再调用replaceAll()。对于正则表达式,您可能想要使用\s\W,这取决于您的确切需求。

英文:

You should look into the replaceAll() method.
It accepts a Regular Expression and replace string.
I would probably first call toLowerCase() and then replaceAll() for whitespaces separated from the special characters. For the Regular Expression you probably want to use \s and \W, depends on your exact requirements.

答案3

得分: 0

A good fit for the requirements is Apache's StringUtils.replaceEach().

it allows to specify multiple search strings and corresponding replacements, and eliminate the need to fiddle with obscure regex patterns.
However, transformation to lowercase is probably easiest with the purpose made String method

String result = StringUtils.replaceEach("HELLO - DI&RIC",
new String[]{" ", "&"},
new String[]{"", "-"}
);
result = result.toLowerCase();

英文:

a good fit for the requirements is Apache's StringUtils.replaceEach()

it allows to specify multiple search strings and corresponding replacements, and eliminate the need to fiddle with obscure regex patterns.
However, transformation to lowercase is probably easiest with the purpose made String method

String result = StringUtils.replaceEach("HELLO - DI&RIC",
    new String[]{" ", "&"},
    new String[]{"",  "-"}
);
result = result.toLowerCase();

huangapple
  • 本文由 发表于 2023年5月15日 14:41:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76251455.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定