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();
4
  • 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. Commented Apr 13, 2016 at 14:41
  • no that's not working Commented Apr 13, 2016 at 14:42
  • Possible duplicate of How to remove duplicate white spaces in string using Java? Commented Apr 13, 2016 at 14:43
  • @Ram This only works with whitespaces at the beginning and end, trim() will not remove inner extra whitespaces String.trim() Commented Apr 13, 2016 at 14:53

1 Answer 1

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+", " "); 
Sign up to request clarification or add additional context in comments.

1 Comment

so how can we remove with whitespaces at the beginning and end and also how to remove inner extra whitespaces

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.