Java 如何在字符串之间添加连字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2637021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:11:22  来源:igfitidea点击:

how to add a hyphen in between a string

java

提问by sarah

I have the following sting xxxxx, I want to add a hyphen like x-xxxx, how can I do so using Java?

我有以下 sting xxxxx,我想添加一个像 x-xxxx 这样的连字符,我如何使用 Java 这样做?

采纳答案by BalusC

You can make use of String#substring().

您可以利用String#substring().

String newstring = string.substring(0, 1) + "-" + string.substring(1);

You'll only need to check the string lengthbeforehand to avoid IndexOutOfBoundsException, but that's nothing more than obvious.

您只需要事先检查字符串长度以避免IndexOutOfBoundsException,但这只不过是显而易见的。

回答by Paulo Guedes

String xxx = "xxxxx";
String hyphened = xxx.substring(0,1) + "-" + xxx.substring(1);

回答by codaddict

You can do:

你可以做:

String orgStr = "xxxxx";
String newStr = orgStr.substring(0,1) + "-" + orgStr.substring(1)

回答by Iain Samuel McLean Elder

Stringis an immutable type in Java, meaning that you can't change the character sequence it represents once the Stringis constructed.

String是 Java 中的不可变类型,这意味着一旦String构造了它就不能更改它所代表的字符序列。

You can use an instance of the StringBuilderclass to create a new instance of Stringthat represents some transformation of the original String. For example, add a hyphen, as you ask, you can do this:

您可以使用StringBuilder该类的一个实例来创建一个String代表原始String. 例如,添加一个连字符,如您所问,您可以这样做:

String str = "xxxxx";
StringBuilder builder = new StringBuilder(str);
builder.insert(1, '-');
String hyphenated = builder.toString(); // "x-xxxx"

The StringBuilderinitially contains a copyof the contents of str; that is, "xxxxx".

StringBuilder最初包含一个拷贝的内容str; 也就是说,"xxxxx"

The call to insertchanges the builder's contents to "x-xxxx".

调用insert将构建器的内容更改为"x-xxxx"

Calling toStringreturns a new Stringcontaining a copythe contents of the string builder.

调用toString返回一个新的String包含字符串构建器内容的副本

Because the Stringtype is immutable, no manipulation of the StringBuilder's contents will ever change the contents of stror hyphenated.

因为String类型是不可变的,对StringBuilder的内容的任何操作都不会改变str或的内容hyphenated

You can change what Stringinstance strrefers toby doing

您可以通过执行更改String实例str所指的内容

str = builder.toString();

instead of

代替

String hyphenated = builder.toString();

But never has the contents of a string that strrefers to changed, because this is not possible. Instead, strused to refer to a instance containing "xxxxx", and now refers to a instance containing "x-xxxx".

但是永远不会str改变所指字符串的内容,因为这是不可能的。相反,str过去指的是包含 的实例"xxxxx",现在指的是包含 的实例"x-xxxx"

回答by JRL

Assuming

假设

String in = "ABCDEF";
String out;

Then, any of:

然后,任何

out = in.replaceFirst(".", "
out = String.format("%1$s-%2$s", in.substring(0,1), in.substring(1));
-");

or

或者

out = in.substring(0,1) + "-" + in.substring(1);

or

或者

out = new StringBuilder(in).insert(1, '-').toString();

or

或者

MaskFormatter fmt = new MaskFormatter("*-****");
fmt.setValueContainsLiteralCharacters(false);
System.out.println(fmt.valueToString("12345"));

will make out = "A-BCDEF".

将使out = "A-BCDEF".

回答by BillS

Here's another way:

这是另一种方式:

##代码##