Waelalmerri
Waelalmerri

Reputation: 147

Adding a whitespace inside a List of strings?

I have an ArrayList of strings which contains different words. What I am trying to do is to add whitespace inside the list between each word.

My code does print out the elements with whitespace, but as printout only, I need the whitespace to be included in the list so I can use it as a value.

Example input: Hi, how are you?, result: Hi,", " ", "are", " ", "you?

public void getCipher(ArrayList<String> list) {
    String regex = "\", \" " + "\", " + "\"";
    for (String input : list) {
        String newResult = input + regex;
        System.out.print(newResult);
    }
}

Upvotes: 1

Views: 1565

Answers (3)

user15402945
user15402945

Reputation:

You can use flatMap method to insert whitespaces between the elements in the list:

public static void main(String[] args) {
    List<String> list = List.of("Hi,", "how", "are", "you?");
    System.out.println(appendSpaces(list));
}
public static List<String> appendSpaces(List<String> list) {
    return list.stream()
            // append a space before each word
            .flatMap(str -> Stream.of(" ", str))
            // skip first space
            .skip(1)
            // return list
            .collect(Collectors.toList());
}

Output:

[Hi,,  , how,  , are,  , you?]

Screenshot from idea:

screenshot from idea


See also: How to add characters in the middle of a word in an arraylist in java?

Upvotes: 0

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79035

Iterate the list and update each string in it with the new string as shown below:

public List<String> updateStrings(List<String> list) {
    String regex = "\", \" " + "\", " + "\"";
    for (int i = 0; i < list.size(); i++) {
        list.set(i, list.get(i) + regex);

    }
    return list;
}

Test it online:

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        // Test
        List<String> list = new ArrayList<>(List.of("Hello", "World"));
        System.out.println(list);
        System.out.println(updateStrings(list));
    }

    static List<String> updateStrings(List<String> list) {
        String regex = "\", \" " + "\", " + "\"";
        for (int i = 0; i < list.size(); i++) {
            list.set(i, list.get(i) + regex);

        }
        return list;
    }
}

Output:

[Hello, World]
[Hello", " ", ", World", " ", "]

enter image description here

Upvotes: 2

Cactusroot
Cactusroot

Reputation: 1058

To add spaces between other items in a list, I would create a new list and iterate over the "old" one, like so:

public ArrayList<String> getCipher(ArrayList<String> list) {
    ArrayList<String> result = new ArrayList<>();
    String regex = "\", \" " + "\", "+ "\"";
    for (String input : list) {
        result.add(input + regex);
        result.add(" ");
    }
    return result;
}

You can set your list to the result of this method, like

list = getCipher(list)

Upvotes: 1

Related Questions