Write a Method
To Delete All Blanks
Java Exercise : How to write a method to delete all
the blanks from the parameters?
Write a method to delete all blanks from its parameter.
The method signature is:
public static String deblank(String s);
Sample output:
deblank("abc") returns "abc" (ie, no changes)
deblank("I'm feeling fine.") returns "I'mfeelingfine."
Solution 1 - Using charAt
public static String deblank(String s) {
String result = "";
for (int i=0; i<s.length(); i++)
{
if (s.charAt(i)
!= ' ') {
result = result + s.charAt(i);
}
}
return result;
}
Solution 2 - Using substring
public static String deblank(String s) {
String result = "";
for (int i=0; i<s.length(); i++)
{
if (!s.substring(i,
i+1).equals(" ")) {
result = result + s.substring(i, i+1);
}
}
return result;
}
Do you have a Java Problem?
Ask It in The Java
Forum
Java Books
Java Certification,
Programming, JavaBean and Object Oriented Reference Books
Return to : Java
Programming Hints and Tips
All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
All product names are trademarks of their respective
companies.
The site www.erpgreat.com is not affiliated with or endorsed
by any company listed at this site.
Every effort is made to ensure the content integrity.
Information used on this site is at your own risk.
The content on this site may not be reproduced
or redistributed without the express written permission of
www.erpgreat.com or the content authors.
|