0

How can i parse a Vector to a normal String?

i.e

Vector newfollowerlist = "name1, name2, name3, name4";

As soon i output the vector string it looks like this;

[name1, name2, name3, name4]

and i want to delete the [] of the line so i thought about parsing the vector string to a normal String and delete the [] by:

String stringWithoutArraySymbols = String VectorHolder.replaceAll("[",""); 
String stringWithoutArraySymbols = String VectorHolder.replaceAll("]","");

How can i actually parse it or is there an easier way to do this?

6
  • 1
    Why on earth are you using a Vector?? This type has been deprecated for a number of decades. And what do you mean by "parse into a String"? You can parse a String into a structure or you can format a structure into a String. Please elaborate... Commented Mar 22, 2014 at 23:21
  • So what should i use else? i just need to store some names and output them without []. Commented Mar 22, 2014 at 23:23
  • Why not write your own logic to output the contents? Also, although an unlikely situation, your current solution would also edit any entry in the vector which contained a square bracket. Commented Mar 22, 2014 at 23:24
  • 1
    I would start here and learn about collections. Commented Mar 22, 2014 at 23:24
  • @ james, thats actually no problem since usernames arent able to hold any []. i really just need to delete them and output em :) Commented Mar 22, 2014 at 23:25

2 Answers 2

2

As I mentioned, do not use Vector. Read about the Java collections API.

I will use the Collection interface.

With Java 8:

final String joined = things.stream().collect(Collectors.joining(","));

With Java 7:

final Iterator<String> iter = things.iterator();
final StringBuilder sb = new StringBuilder();
sb.append(iter.next());
while (iter.hasNext()) {
    sb.append(",").append(iter.next());
}
final String joined = sb.toString();
Sign up to request clarification or add additional context in comments.

Comments

1

A slight variation

String s = newfollowerlist.toString();
System.out.println(s.substring(1, s.length()-1));

You know the format of newfollowerlist.toString() so its easy enough to get your desired format. Should work if newfollowerlist is a Vector, List, Set or many other Collection types.

2 Comments

I would argue that encouraging the OP to use the toString method of Vector to "parse" a Vector is plain absurd. This answer is a dirty hack and the OP will find that in some random Java update this will break. Spectacularly.
The format is defined in the javadoc for AbstractCollection so officially part of the API. docs.oracle.com/javase/1.5.0/docs/api/java/util/… Curiously the javadoc also mentions how its implemented, which is exactly the same algorithm as your solution, apart from a space after the comma.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.