简体   繁体   中英

How to Remove White space after splitting String?

Hi i am splitting String with delimiter "," . The result containing white space. How can i remove this white space? why this white space is coming?

public class StringUtilsPractice  {
public static void main(String[] args) {
    String myString= "123,456";
    String[] splitedString= myString.split(",");
    List<String> splitedList= Arrays.asList(splitedString);
    List<String> filterValue=new ArrayList<String>();
    filterValue.add("123,456");
    boolean flag=filterValue.contains(splitedList);
    System.out.println("splitedList:"+splitedList);
    System.out.println("filterValue:"+filterValue);
    System.out.println("Flag::"+flag);
}

}

Result: splitedList:[123, 456] filterValue:[123,456] Flag::false

I am splitting by default one white space is adding. how can remove that white space. And result should return "true".

Look at line 442 of AbstractCollection . What you're looking at is the native toString() output of List (ie, the string representation of List ), which adds a comma for readability.

You can also see this in the JavaDoc for AbstractCollection :

Returns a string representation of this collection. The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space) . Elements are converted to strings as by String.valueOf(Object) . (emphasis mine)

If you iterated over the values yourself and printed them out, you will notice that there are no spaces.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM