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
how to add a hyphen in between a string
提问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
String
is an immutable type in Java, meaning that you can't change the character sequence it represents once the String
is constructed.
String
是 Java 中的不可变类型,这意味着一旦String
构造了它就不能更改它所代表的字符序列。
You can use an instance of the StringBuilder
class to create a new instance of String
that 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 StringBuilder
initially contains a copyof the contents of str
; that is, "xxxxx"
.
在StringBuilder
最初包含一个拷贝的内容str
; 也就是说,"xxxxx"
。
The call to insert
changes the builder's contents to "x-xxxx"
.
调用insert
将构建器的内容更改为"x-xxxx"
。
Calling toString
returns a new String
containing a copythe contents of the string builder.
调用toString
返回一个新的String
包含字符串构建器内容的副本。
Because the String
type is immutable, no manipulation of the StringBuilder
's contents will ever change the contents of str
or hyphenated
.
因为String
类型是不可变的,对StringBuilder
的内容的任何操作都不会改变str
或的内容hyphenated
。
You can change what String
instance str
refers toby doing
您可以通过执行更改String
实例str
所指的内容
str = builder.toString();
instead of
代替
String hyphenated = builder.toString();
But never has the contents of a string that str
refers to changed, because this is not possible. Instead, str
used 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:
这是另一种方式:
##代码##