将数组转换为字符串,不带括号,逗号或空格

时间:2021-11-11 21:43:47

I'm trying to convert an array list of individual letters (e.g. {"a", "b", "c"}) into a string that is that is made from all those letters combined — in order — to make a word. So with the example above, the array would be {"a", "b", "c"} and the word would be "abc". I'm currently using the code below:

我正在尝试将单个字母的数组列表(例如{“a”,“b”,“c”})转换为一个字符串,该字符串是由所有这些字母组合而成 - 按顺序 - 来创建一个单词。因此,通过上面的示例,数组将是{“a”,“b”,“c”},而单词将是“abc”。我目前正在使用以下代码:

ArrayList<String> arr = new ArrayList<String>();
String str = "a"; //this is just to define it before hand with a random value
*after a lot more code*
str = Arrays.toString(arr.toArray()).replace("[", "").replace("]", "").replace(",", "");
System.out.println(str);

This works — I get my result without brackets or commas, but since I replaced the commas and brackets with spaces, I get "a b c" instead of "abc". Is there a way to change this so I can replace the commas with purely nothing instead of spaces? I guess a better way to phrase this would be to completely remove the commas and brackets.

这有效 - 我得到的结果没有括号或逗号,但由于我用空格替换逗号和括号,我得到“a b c”而不是“abc”。有没有办法改变这个,所以我可以用纯粹的空格而不是空格替换逗号?我想用一种更好的方式来表达这一点就是完全删除逗号和括号。

Thanks so much for any help.

非常感谢您的帮助。

4 个解决方案

#1


3  

You could use a Stream and Collectors.joining() like

您可以使用Stream和Collectors.joining()之类的

String[] arr = { "a", "b", "c" };
System.out.println(Stream.of(arr).collect(Collectors.joining()));

which outputs (as requested)

哪些产出(按要求)

abc

#2


3  

Assuming you're looking to strip down anything from the input string that is not an alphabetic character, you can use this :

假设您要从输入字符串中删除不是字母字符的任何内容,您可以使用:

    StringBuilder sb = new StringBuilder(); 

    for (int i = 0; i < input.length(); i++) {
        if (Character.isAlphabetic(input.charAt(i))) {
            sb.append(input.charAt(i));
        }
    } 

    return sb.toString();

#3


2  

String[] sa = new String[] {"a", "b", "c"};
System.out.println(String.join("", sa));

The Stream API is defined in JDK1.8

Stream API在JDK1.8中定义

#4


1  

When you call:

你打电话时:

System.out.println(stringOne);

What is really happening is System.out.println implicitly calls the toString() method of the array to obtain its string representation:

实际发生的是System.out.println隐式调用数组的toString()方法来获取其字符串表示:

System.out.println(stringOne.toString());

The implementation of Array.toString() returns the array elements (or rather, the toString() representation of the array elements), comma separated.

Array.toString()的实现返回数组元素(或者更确切地说,数组元素的toString()表示),逗号分隔。

If you would like a different representation you have two options. You can create a new array class that extends the base array class, and overrides the toString() method to return the desired string representation. This is .. probably not what you want to do, but it is an option.

如果您想要不同的表示,您有两种选择。您可以创建一个扩展基类数组的新数组类,并覆盖toString()方法以返回所需的字符串表示形式。这可能不是你想要做的,但它是一个选择。

Alternatively, you can process the array yourself and construct the desired string representation. @Elliott Frisch shows a nice way to do this using the Streams API available in Java 8.

或者,您可以自己处理数组并构造所需的字符串表示形式。 @Elliott Frisch使用Java 8中提供的Streams API显示了一种很好的方法。

There are many ways other ways you could achieve the same result. Here's a quick and dirty for loop implementation:

您可以通过多种方式获得相同的结果。这是一个快速而又脏的for循环实现:

StringBuilder arrayAsString = new StringBuilder();
String delimiter = "";
for(String element : arr) {
    arrayAsString.append(delimiter).append(element);
    delimiter = ","; // I hate this idiom but it's handy for joining strings when you don't have a better .join() method available.
}

System.out.println(arrayAsString.toString()); // calling toString() explicitly for clarity, but it's not necessary

#1


3  

You could use a Stream and Collectors.joining() like

您可以使用Stream和Collectors.joining()之类的

String[] arr = { "a", "b", "c" };
System.out.println(Stream.of(arr).collect(Collectors.joining()));

which outputs (as requested)

哪些产出(按要求)

abc

#2


3  

Assuming you're looking to strip down anything from the input string that is not an alphabetic character, you can use this :

假设您要从输入字符串中删除不是字母字符的任何内容,您可以使用:

    StringBuilder sb = new StringBuilder(); 

    for (int i = 0; i < input.length(); i++) {
        if (Character.isAlphabetic(input.charAt(i))) {
            sb.append(input.charAt(i));
        }
    } 

    return sb.toString();

#3


2  

String[] sa = new String[] {"a", "b", "c"};
System.out.println(String.join("", sa));

The Stream API is defined in JDK1.8

Stream API在JDK1.8中定义

#4


1  

When you call:

你打电话时:

System.out.println(stringOne);

What is really happening is System.out.println implicitly calls the toString() method of the array to obtain its string representation:

实际发生的是System.out.println隐式调用数组的toString()方法来获取其字符串表示:

System.out.println(stringOne.toString());

The implementation of Array.toString() returns the array elements (or rather, the toString() representation of the array elements), comma separated.

Array.toString()的实现返回数组元素(或者更确切地说,数组元素的toString()表示),逗号分隔。

If you would like a different representation you have two options. You can create a new array class that extends the base array class, and overrides the toString() method to return the desired string representation. This is .. probably not what you want to do, but it is an option.

如果您想要不同的表示,您有两种选择。您可以创建一个扩展基类数组的新数组类,并覆盖toString()方法以返回所需的字符串表示形式。这可能不是你想要做的,但它是一个选择。

Alternatively, you can process the array yourself and construct the desired string representation. @Elliott Frisch shows a nice way to do this using the Streams API available in Java 8.

或者,您可以自己处理数组并构造所需的字符串表示形式。 @Elliott Frisch使用Java 8中提供的Streams API显示了一种很好的方法。

There are many ways other ways you could achieve the same result. Here's a quick and dirty for loop implementation:

您可以通过多种方式获得相同的结果。这是一个快速而又脏的for循环实现:

StringBuilder arrayAsString = new StringBuilder();
String delimiter = "";
for(String element : arr) {
    arrayAsString.append(delimiter).append(element);
    delimiter = ","; // I hate this idiom but it's handy for joining strings when you don't have a better .join() method available.
}

System.out.println(arrayAsString.toString()); // calling toString() explicitly for clarity, but it's not necessary