0

I want to remove empty space from the first String and make it look like the second String.

1)EVANS MD                 JOSEPH       J
2)EVANS MD JOSEPH J

My code:

String finaloriginAutoCompleteText = providerOriginAddress.get(position).getProviderName();
gar
  • 14,152
  • 5
  • 30
  • 31
Krish
  • 4,166
  • 11
  • 58
  • 110
  • finaloriginAutoCompleteText.trim(); explanation: It returns a copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space. – Ram Prakash Bhat Apr 13 '16 at 14:41
  • no that's not working – Krish Apr 13 '16 at 14:42
  • Possible duplicate of [How to remove duplicate white spaces in string using Java?](http://stackoverflow.com/questions/3958955/how-to-remove-duplicate-white-spaces-in-string-using-java) – Laur Ivan Apr 13 '16 at 14:43
  • @Ram This only works with whitespaces at the beginning and end, trim() will not remove inner extra whitespaces [String.trim()](http://developer.android.com/reference/java/lang/String.html#trim()) – Slobodan Antonijević Apr 13 '16 at 14:53

1 Answers1

1
String finaloriginAutoCompleteText;
finaloriginAutoCompleteText = providerOriginAddress.get(position).getProviderName();

// trim the string (for leading and ending whitespaces) and then
// replace all remaining (inner) multiple spaces (\s+) with a single space (" ")
finaloriginAutoCompleteText = finaloriginAutoCompleteText.trim().replaceAll("\\s+", " "); 
Slobodan Antonijević
  • 2,533
  • 2
  • 17
  • 27
  • so how can we remove with whitespaces at the beginning and end and also how to remove inner extra whitespaces – Krish Apr 13 '16 at 14:57