在Java中添加空格

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

Adding space in Java

问题

所以我正在尝试练习找出在字符值之间添加空格的最快最简单的方法。

public static void main(String[] args) {      
        char arr[] = {'g', 'a', 'p', 'e', 't'};
        
        System.out.println("数组: " + new String(arr)); //预期 g a p e t
                                                     //实际得到 gapet
    }

我尝试将字符值转换为字符串,但仍然无法弄清楚如何在每个字母之间添加空格。

英文:

So I am trying to practice figure out the quickest and easiest way to add spaces in between the character values.

public static void main(String[] args) {      
        char arr[] = {'g', 'a', 'p', 'e', 't'};
        
        System.out.println("Array: " + new String(arr)); //expected g a p e t
                                                         //instead I got gapet
    }

I tried converting the char values as a string, but I still can't figure out to add spaces in between each letter.

答案1

得分: 1

这是我会采取的方法

public static void main(String[] args) {
    char arr[] = {'g', 'a', 'p', 'e', 't'};

    // 将字符数组转换为带有空格的字符串
    String result = String.join(" ", new String(arr).split(""));

    System.out.println("Array: " + result); // 输出:g a p e t
}
英文:

This would be the approach I would take

public static void main(String[] args) {
    char arr[] = {'g', 'a', 'p', 'e', 't'};

    // Convert the char array to a string with spaces in between
    String result = String.join(" ", new String(arr).split(""));

    System.out.println("Array: " + result); // Output: g a p e t
}

答案2

得分: 1

你可以按如下方式循环遍历字符数组:

public static void main(String[] args) 
{
    char arr[] = {'g', 'a', 'p', 'e', 't'};

    for (char i : arr)
    {
      System.out.print(i + " ");
    }
}
英文:

You can loop through the array of characters as follows

public static void main(String[] args) 
{
    char arr[] = {'g', 'a', 'p', 'e', 't'};

    for (char i : arr)
    {
      System.out.print(i + " ");
    }
  }
}

答案3

得分: 1

有几种方法。

最简单的方法是使用一个 for 循环

String string = "";
int index = 0;
for (char character : arr) {
    if (index++ != 0) string += " ";
    string += character;
}

或者,正如其他人提到的,您可以使用 String#split 方法,然后使用 String#join 方法。

String string = String.join(" ", new String(arr).split(""));

您还可以使用 StringBuilder,利用 insert 方法。

StringBuilder string = new StringBuilder(new String(arr));
for (int index = string.length() - 1; index >= 0; index--)
    string.insert(index, ' ');

最后,您可以谨慎使用 String#replace。请注意,这会在开头和结尾添加额外的 空格 字符。您可以使用 String#trim 来移除它们。

String string = new String(arr).replace("", " ").trim();
英文:

There are a few approaches.

The simplest would be to use a for-loop.

String string = "";
int index = 0;
for (char character : arr) {
    if (index++ != 0) string += " ";
    string += character;
}

Or, as others have mentioned, you can use the String#split method, and then use the String#join method.

String string = String.join(" ", new String(arr).split(""));

You could, also, use a StringBuilder, utilizing the insert method.

StringBuilder string = new StringBuilder(new String(arr));
for (int index = string.length() - 1; index >= 0; index--)
    string.insert(index, ' ');

Finally, you could use, with caution, a String#replace.
Note that, this will place an additional space character at the beginning, and end.
You can use, String#trim to remove them.

String string = new String(arr).replace("", " ").trim();

答案4

得分: 0

让StringJoiner为您处理工作:

public static void main(String[] args) {
    char arr[] = {'g', 'a', 'p', 'e', 't'};
    StringJoiner joiner = new StringJoiner(" ");
    for (char c : arr) {
        joiner.add(String.valueOf(c));
    }
    System.out.println(joiner.toString());
}
英文:

Let the StringJoiner do the work for you:

public static void main(String[] args) {
    char arr[] = {'g', 'a', 'p', 'e', 't'};
    StringJoiner joiner = new StringJoiner(" ");
    for (char c : arr) {
        joiner.add(String.valueOf(c));
    }
    System.out.println(joiner.toString());
}

答案5

得分: 0

这是使用基本Java的实现。我在末尾添加了一个单独的打印语句以避免数组末尾的空格。

public static void main(String[] args) 
{
    char arr[] = {'g', 'a', 'p', 'e', 't'};

    for (int i=0; i<arr.size()-1; i++)
    {
      System.out.print(arr[i] + " ");
    }
    System.out.print(arr[arr.size()-1]);
}
英文:

Here's an implementation using basic Java. I've added a separate print statement at end to avoid space at the end of array.

public static void main(String[] args) 
{
    char arr[] = {&#39;g&#39;, &#39;a&#39;, &#39;p&#39;, &#39;e&#39;, &#39;t&#39;};

    for (int i=0; i&lt;arr.size()-1; i++)
    {
      System.out.print(arr[i] + &quot; &quot;);
    }
    System.out.print(arr[arr.size()-1]);
  }
}

答案6

得分: 0

在循环后,对结果字符串调用了trim()方法以去除任何尾部空白字符。最终,打印出字符串,输出为:"Array: g a p e t"。

英文:

After the loop, the trim() method is called on the resulting string to remove any trailing whitespace. Finally, the string is printed, resulting in the output: "Array: g a p e t".

public static void main(String[] args) {
    char arr[] = { &#39;g&#39;, &#39;a&#39;, &#39;p&#39;, &#39;e&#39;, &#39;t&#39; };

    StringBuilder sb = new StringBuilder();
    for (char c : arr) {
        sb.append(c).append(&quot; &quot;);
    }
    String result = sb.toString().trim();

    System.out.println(&quot;Array: &quot; + result);
}

huangapple
  • 本文由 发表于 2023年6月9日 02:58:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434935.html
匿名

发表评论

匿名网友

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

确定