0

I'm trying to return and output the contents of a vector as a cohesive String. I tried vector.toString() but that would also include brackets and commas. I can't have that. Is there a way to convert just the elements of that vector into a String?

Woodchuck
  • 3,869
  • 2
  • 39
  • 70
Merna George
  • 19
  • 1
  • 4
  • 6
    Help us help you - please provide a sample of such a vector and the result you'd lie to get for it – Mureinik Dec 26 '19 at 20:46
  • 1
    @Merna George: Welcome to StackOverflow..!! Please post question outlining your problem and detailing what you have tried and what you are struggling to achieve with some examples if required. – Faiz Kidwai Dec 26 '19 at 21:03

2 Answers2

1
  • You can do this by using apache StringUtils:

    import java.util.Vector;
    
    import org.apache.commons.lang3.StringUtils;
    
    public class VectorExample {
    
     public static void main(String[] args) {
    
      Vector<String> vector=  new Vector<String>();
      vector.add("test1");
      vector.add("test2");
      vector.add("test3");
    
      System.out.println(StringUtils.join(vector, " "));
     }
    }
    

Output: test1 test2 test3

  • I would suggest adding a new method like getElementsAsString() in a utility/helper class or into the class where you have created/processing the vector, which will get you the string in the format you want.

  • If you want to stick to and use only the toString() method, extending the Vector is an option. Then you can override the toString() method.

Thanks.

Mehul Gayate
  • 344
  • 3
  • 7
  • If you're using Java 8+ then you don't have to pull in a dependency for this, just use [`String#join(CharSequence,Iterable)`](https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/lang/String.html#join(java.lang.CharSequence,java.lang.Iterable)). – Slaw Dec 26 '19 at 21:27
0

You can do it as follows:

import java.util.Vector;

public class Main {
    public static void main(String args[]) {
        Vector<Integer> vector = new Vector<Integer>();
        vector.add(1);
        vector.add(2);
        vector.add(3);

        StringBuilder sb = new StringBuilder();
        vector.forEach(n -> sb.append(String.valueOf(n)));
        String str = sb.toString();

        System.out.println(sb);
        System.out.println(str);
    }
}

Output:

123
123

Alternatively,

import java.util.Vector;

public class Main {
    public static void main(String args[]) {
        Vector<Integer> vector = new Vector<Integer>();
        vector.add(1);
        vector.add(2);
        vector.add(3);

        StringBuilder sb = new StringBuilder();
        for (int n : vector) {
            sb.append(n);
        }
        String str = sb.toString();

        System.out.println(sb);
        System.out.println(str);
    }
}

Output:

123
123

Alternatively,

import java.util.Vector;

public class Main {
    public static void main(String args[]) {
        Vector<Integer> vector = new Vector<Integer>();
        vector.add(1);
        vector.add(2);
        vector.add(3);

        String str = "";
        for (int n : vector) {
            str += n;
        }

        System.out.println(str);
    }
}

Output:

123

Note: This approach should be discouraged because it will create as many String objects as the number of iterations.


Alternatively,

import java.util.Vector;

public class Main {
    public static void main(String args[]) {
        Vector<Integer> vector = new Vector<Integer>();
        vector.add(1);
        vector.add(2);
        vector.add(3);

        String str = vector.toString().replaceAll(",|\\[|\\]|\\s+", "");

        System.out.println(str);
    }
}

Output:

123

If the elements of the Vector are String, you can also do the following:

import java.util.Vector;

public class Main {
    public static void main(String args[]) {
        Vector<String> vector = new Vector<String>();
        vector.add("1");
        vector.add("2");
        vector.add("3");

        String str = String.join("", vector);

        System.out.println(str);
    }
}

Output:

123
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • No reason to create a `Stream` from the `Vector` if all you're going to do is `forEach`, just use `vector.forEach(...)` directly. – Slaw Dec 26 '19 at 21:28