Home > Archive > Java Help > October 2005 > Strings
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Frances Del Rio 2005-10-28, 7:02 pm |
| Please, how can I delete all occurences of one char in a string? I
looked @ all methods for String and StringBuffer; StringBuffer has
delete(), but method takes as arguments a start and an endpoint in
string.. what I want to do is, for example, delete all periods in a
string, or all dashes, etc..
thank you,
Frances
| |
| Knute Johnson 2005-10-28, 7:02 pm |
| Frances Del Rio wrote:
> Please, how can I delete all occurences of one char in a string? I
> looked @ all methods for String and StringBuffer; StringBuffer has
> delete(), but method takes as arguments a start and an endpoint in
> string.. what I want to do is, for example, delete all periods in a
> string, or all dashes, etc..
>
> thank you,
> Frances
Frances:
Look at String.replaceAll();
Here is a demonstration program.
public class test {
public static void main(String[] args) throws Exception {
String str = "hello there.";
String newStr = str.replaceAll("\\.","");
System.out.println(newStr);
}
}
--
Knute Johnson
email s/nospam/knute/
| |
| Frances 2005-10-28, 9:56 pm |
| Knute Johnson wrote:
> Frances Del Rio wrote:
>
>
>
> Frances:
>
> Look at String.replaceAll();
>
> Here is a demonstration program.
>
> public class test {
> public static void main(String[] args) throws Exception {
> String str = "hello there.";
> String newStr = str.replaceAll("\\.","");
> System.out.println(newStr);
> }
> }
thank you very much!!
Frances
| |
| Roedy Green 2005-10-28, 9:56 pm |
| On Fri, 28 Oct 2005 17:32:45 -0400, Frances Del Rio <fdr58@yahoo.com>
wrote, quoted or indirectly quoted someone who said :
>Please, how can I delete all occurences of one char in a string?
If you wanted to do this with your own code it would look like this:
public class Stripper
{
/**
* Strips out the given char from a String.
* This should be quicker than other ways of getting the same
effect.
* See also String.replace and String.replaceAll.
* @param charToStrip the char e.g. ' ', '/', that you no longer
want in your String .
* @param s the String, may be empty but not null.
* @return new String with all instances of charToStrip removed, or
the original
* String if there were no instances of charToStrip.
*/
public static String stripper( char charToStrip, String s )
{
int place = s.indexOf( charToStrip );
if ( place < 0 )
{
// were no instances, so we can return the original String.
return s;
}
int length = s.length();
// we know result will be at least one shorter.
StringBuilder sb = new StringBuilder( length-1 );
// append first part of s, before first instance of unwanted
char
sb.append( s.substring( 0, place ) );
// copy over tail end of string, ignoring unwanted chars.
for ( int i=place+1; i<length; i++ )
{
char c = s.charAt( i );
if ( c != charToStrip )
{
sb.append( c );
}
} // end for
return sb.toString();
}
/**
* test harness
*
* @param args not used
*/
public static void main ( String[] args )
{
System.out.println ( stripper( ' ', "hello world" ) ); //
helloworld
System.out.println ( stripper( ' ', "h e l l o w o r l d " )
); // helloworld
System.out.println ( stripper( 'q', "fine as is" ) ); // fine as
is
}
} // end class
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
|
|
|
|
|