如何在字符串中的字符左右添加空格?
How to add spaces to left and right of chars in a string?
我正在尝试打印出一个字符串,其中每个字符的两边都带有 spaces
所以如果我有
String s = "abcde"
它会创建这样的东西
a b c d e
第一个字符前有一个 space,每个字符之间有三个。
我只是无法根据我的知识找到一种方法。
一个超级简单的示例,无法处理大量潜在的输入场景。
public static void main(String[] args)
{
String s = "abcde";
for (int i = 0; i < s.length(); ++i) {
System.out.print("_" + s.charAt(i));
}
System.out.println("_");
}
注意: 使用下划线而不是 space 以允许对输出进行目视检查。
示例输出:
_a_b_c_d_e_
而不是直接输出,例如,可以使用 StringBuilder
和 .append
到构建器。
使用 StringBuilder:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); ++i) {
sb.append('_').append(s.charAt(i));
}
sb.append('_');
System.out.println(sb.toString());
根据评论,其中所需的输出略有不同(两个内部 spaces,一个前导和尾随 space),这表明了另一种方法:
public static String addSpace(String inp) {
StringBuilder sB = new StringBuilder();
String string = inp.trim();
String div = "___"; // spaces, or whatever
sB.append('_'); // add leading space
for(int index = 0; index < string.length(); ++index) {
sB.append(string.charAt(index))
.append(div); // two spaces
}
sB.setLength(sB.length() - (div.length() - 1) );
return (sB.toString());
}
注意: 再次使用下划线以便于调试。
div
设置为3个下划线(或spaces)时的输出:
_0___0___0___1___0___1___1___0_
您可以定义一个空字符串:result = “”;
然后使用函数toCharArray()
使用foreach循环遍历要打印的字符串
(字符字符:str.toCharArray())
在这个循环中执行 ->
结果+=“”+字符;
更新
更新要求:
I failed to realize that I need something that add one place in front
of the first term and then 3 spaces between each term.
_0___0___0___0___0_
for example.
对于更新后的要求,您可以使用另一个很酷的东西,String#join
。
public class Main {
public static void main(String[] args) {
String s = "abcde";
String result = "_" + String.join("___", s.split("")) + "_";
System.out.println(result);
}
}
输出:
_a___b___c___d___e_
原回答
可以有很多种方法。我发现使用正则表达式更容易做到这一点:
public class Main {
public static void main(String[] args) {
String s = "abcde";
String result = s.replaceAll(".", " [=12=] ");
System.out.println(result);
}
}
输出:
a b c d e
正则表达式,.
匹配单个字符,[=19=]
将此匹配替换为 space + 匹配 + space.
另一个很酷的方法是使用 Stream API.
import java.util.Arrays;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
String s = "abcde";
String result = Arrays.stream(s.split(""))
.map(str -> " " + str + " ")
.collect(Collectors.joining());
System.out.println(result);
}
}
输出:
a b c d e
String result = s.chars().mapToObj(
Character::toString
).collect(Collectors.joining(" "));
类似于循环版本,但使用流。
实现此目的的另一个方法是将 String 拆分为字符的 String[] 并通过 space:
连接它们
public class Main {
public static void main(String[] args) {
String s = "abcde";
System.out.println(" " + String.join(" ", s.split("")) + " ");
}
}
输出:
a b c d e
编辑:
上面的代码不适用于具有 Unicode 代码点(如“ab”)的字符串,因此不应拆分空字符串,而是应在正则表达式上执行拆分:"(?<=.)"
.
public class Main {
public static void main(String[] args) {
String s = "abcde";
System.out.println(" " + String.join(" ", s.split("(?<=.)")) + " ");
}
}
感谢@saka1029 指出这一点。
您可以使用带有三个参数的 Collectors.joining(delimiter,prefix,suffix)
方法:
String s1 = "abcde";
String s2 = Arrays.stream(s1.split(""))
.collect(Collectors.joining("_+_", "-{", "}-"));
System.out.println(s2); // -{a_+_b_+_c_+_d_+_e}-
另请参阅:
我正在尝试打印出一个字符串,其中每个字符的两边都带有 spaces
所以如果我有
String s = "abcde"
它会创建这样的东西
a b c d e
第一个字符前有一个 space,每个字符之间有三个。
我只是无法根据我的知识找到一种方法。
一个超级简单的示例,无法处理大量潜在的输入场景。
public static void main(String[] args)
{
String s = "abcde";
for (int i = 0; i < s.length(); ++i) {
System.out.print("_" + s.charAt(i));
}
System.out.println("_");
}
注意: 使用下划线而不是 space 以允许对输出进行目视检查。
示例输出:
_a_b_c_d_e_
而不是直接输出,例如,可以使用 StringBuilder
和 .append
到构建器。
使用 StringBuilder:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); ++i) {
sb.append('_').append(s.charAt(i));
}
sb.append('_');
System.out.println(sb.toString());
根据评论,其中所需的输出略有不同(两个内部 spaces,一个前导和尾随 space),这表明了另一种方法:
public static String addSpace(String inp) {
StringBuilder sB = new StringBuilder();
String string = inp.trim();
String div = "___"; // spaces, or whatever
sB.append('_'); // add leading space
for(int index = 0; index < string.length(); ++index) {
sB.append(string.charAt(index))
.append(div); // two spaces
}
sB.setLength(sB.length() - (div.length() - 1) );
return (sB.toString());
}
注意: 再次使用下划线以便于调试。
div
设置为3个下划线(或spaces)时的输出:
_0___0___0___1___0___1___1___0_
您可以定义一个空字符串:result = “”;
然后使用函数toCharArray()
使用foreach循环遍历要打印的字符串(字符字符:str.toCharArray())
在这个循环中执行 ->
结果+=“”+字符;
更新
更新要求:
I failed to realize that I need something that add one place in front of the first term and then 3 spaces between each term.
_0___0___0___0___0_
for example.
对于更新后的要求,您可以使用另一个很酷的东西,String#join
。
public class Main {
public static void main(String[] args) {
String s = "abcde";
String result = "_" + String.join("___", s.split("")) + "_";
System.out.println(result);
}
}
输出:
_a___b___c___d___e_
原回答
可以有很多种方法。我发现使用正则表达式更容易做到这一点:
public class Main {
public static void main(String[] args) {
String s = "abcde";
String result = s.replaceAll(".", " [=12=] ");
System.out.println(result);
}
}
输出:
a b c d e
正则表达式,.
匹配单个字符,[=19=]
将此匹配替换为 space + 匹配 + space.
另一个很酷的方法是使用 Stream API.
import java.util.Arrays;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
String s = "abcde";
String result = Arrays.stream(s.split(""))
.map(str -> " " + str + " ")
.collect(Collectors.joining());
System.out.println(result);
}
}
输出:
a b c d e
String result = s.chars().mapToObj(
Character::toString
).collect(Collectors.joining(" "));
类似于循环版本,但使用流。
实现此目的的另一个方法是将 String 拆分为字符的 String[] 并通过 space:
连接它们public class Main {
public static void main(String[] args) {
String s = "abcde";
System.out.println(" " + String.join(" ", s.split("")) + " ");
}
}
输出:
a b c d e
编辑:
上面的代码不适用于具有 Unicode 代码点(如“ab”)的字符串,因此不应拆分空字符串,而是应在正则表达式上执行拆分:"(?<=.)"
.
public class Main {
public static void main(String[] args) {
String s = "abcde";
System.out.println(" " + String.join(" ", s.split("(?<=.)")) + " ");
}
}
感谢@saka1029 指出这一点。
您可以使用带有三个参数的 Collectors.joining(delimiter,prefix,suffix)
方法:
String s1 = "abcde";
String s2 = Arrays.stream(s1.split(""))
.collect(Collectors.joining("_+_", "-{", "}-"));
System.out.println(s2); // -{a_+_b_+_c_+_d_+_e}-
另请参阅: