Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2004

Tip: Looking for answers? Try searching our database.

Plucking a String

Thread view: 
Ben - 09 Mar 2004 14:56 GMT
Hi,

What would be the most optimised means of removing a specific set of
printable characters from a String?

I was thinking along the lines of using the String method replaceAll() with
an array of Strings containing all the characters I want removed in the
array.  Then use a loop to iterate through the input'ed String in conjuction
with my String array of characters i don't want, removing the characters and
rewriting it to a new String object.

String input = new String("JAVA IS COOL");
String[] vowels  = new String[5];
String consonantsOnly = new String(input);

// Array of Vowels
vowels[0] = "A";
vowels[1] = "E";
vowels[2] = "I";
vowels[3] = "O";
vowels[4] = "U";

for (int i = 0; i < 5; i++) {
   consonantsOnly = consonantsOnly.replaceAll(vowels[i], "");
}

System.out.println(consonantsOnly);

Thanks in advance,
Ben.
Andrew Thompson - 09 Mar 2004 15:38 GMT
> What would be the most optimised means of removing a specific set of
> printable characters from a String?

Dunno about _best_, but this is short.

> String input = new String("JAVA IS COOL");
> String[] vowels  = new String[5];

String vowels[] = input.split("AEIOU");

Signature

Andrew Thompson
* http://www.PhySci.org/ Open-source software suite
* http://www.PhySci.org/codes/ Web & IT Help
* http://www.1point1C.org/ Science & Technology

Ben - 09 Mar 2004 16:04 GMT
Hi Andrew,

Unfortunately, split() just asks for a delimeter character to "split" a
string into an Array of Strings.

String vowels[] = input.split("AEIOU");

The above is trying to find the delimeter character "AEIOU" to split the
String "input" into an array of Strings. For example:

String input = "Ben,John,Andrew,Peter,Aaron";
String names[] = input.split(",");

for(int i = 0; i < names.length; i++)
  System.out.println(names[i]);

Should dump the names in input, each on a new line.

I'm want to remove certain characters from my string, not dice it up.  But
thanks anyway, you alerted me to a method I've ignored :-)

Adam.

> > What would be the most optimised means of removing a specific set of
> > printable characters from a String?
[quoted text clipped - 11 lines]
> * http://www.PhySci.org/codes/ Web & IT Help
> * http://www.1point1C.org/ Science & Technology
Thomas Schodt - 09 Mar 2004 16:27 GMT
> Unfortunately, split() just asks for a delimeter character to "split" a
> string into an Array of Strings.
[quoted text clipped - 3 lines]
> The above is trying to find the delimeter character "AEIOU" to split the
> String "input" into an array of Strings.

I presume Andrew meant to say

String[] noVowels = input.split("[AEIOUaeiou]+");
Andrew Thompson - 09 Mar 2004 16:41 GMT
>> Unfortunately, split() just asks for a delimeter character to "split" a
>> string into an Array of Strings.
[quoted text clipped - 7 lines]
>
> String[] noVowels = input.split("[AEIOUaeiou]+");

<psst>
I would have, ..if I understood regex's.    ;-)
</psst>

Signature

Andrew Thompson
* http://www.PhySci.org/ Open-source software suite
* http://www.PhySci.org/codes/ Web & IT Help
* http://www.1point1C.org/ Science & Technology

Ben - 11 Mar 2004 08:28 GMT
Thank you Thomas and Andrew.  Much appreciated.

I probably should start a new thread for this one so everyone can see, but,
how does the regex of "[AEIOUaeiou]+" work.  Why did Andrew's fail?

Regards,
Ben

> > Unfortunately, split() just asks for a delimeter character to "split" a
> > string into an Array of Strings.
[quoted text clipped - 7 lines]
>
> String[] noVowels = input.split("[AEIOUaeiou]+");
Andrew Thompson - 11 Mar 2004 23:03 GMT
> I probably should start a new thread for this one so everyone can see, but,
> how does the regex of "[AEIOUaeiou]+" work.  

regex's present a quite powerful and subtle
way to search files, they have characters to
indicate things like 'any number of occurences of..',
'only if occurs after..' and such.

>..Why did Andrew's fail?

Because it was anything but subtle, and
..well, just plain *wrong* for what you wanted!

I hope someone can provide a good link to
a regex explanation.  I will be checking it
out myself.   :-)

Signature

Andrew Thompson
* http://www.PhySci.org/ Open-source software suite
* http://www.PhySci.org/codes/ Web & IT Help
* http://www.1point1C.org/ Science & Technology

FISH - 12 Mar 2004 11:17 GMT
> Thank you Thomas and Andrew.  Much appreciated.
>
[quoted text clipped - 3 lines]
> Regards,
> Ben

Regexp's are very powerful text matching patterns, and well worth
learning if you have the time.  They are, however, quite involved
and not for the faint hearted.  The example given above is a very
basic novice-level expression.  They can get a lot more scarey! :-)

The split method accepts a re which it attempts to match in order
to divide up a String.  In our example we wish to divide up on
vowels, case insensitive.  The [ ... ] construct allows us to
specify a set (or 'class') of characters for the pattern.  It will
act like a giant OR switch: A or E or I or O .....  Without the
[ ] the re will attempt to match the actual string "AEIOUaeiou".

The trailing + specifies that the previous construct (the class)
can appear one or more times - meaning that the re will gobble up
as many vowels as it can in a single match.  This ensures that
sequences like ZZaaXX result in "ZZ" "XX" and not "ZZ" "" "XX".
However, the split() method discards empty strings anyway, so the
Javadocs say, so the trailing + is probably not needed (I think!)
but wise to include anyway just for readability sake.

-FISH-   ><
Roedy Green - 17 Mar 2004 01:56 GMT
>Regexp's are very powerful text matching patterns, and well worth
>learning if you have the time.  They are, however, quite involved
>and not for the faint hearted.  The example given above is a very
>basic novice-level expression.  They can get a lot more scarey! :-)

for some examples to get you going see
http://mindprod.com/jgloss/regex.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Andrew Thompson - 17 Mar 2004 06:56 GMT
> http://mindprod.com/jgloss/regex.html

Nice page Roedy.  Bookmarked for reference.

Signature

Andrew Thompson
* http://www.PhySci.org/ Open-source software suite
* http://www.PhySci.org/codes/ Web & IT Help
* http://www.1point1C.org/ Science & Technology

Daniel Sjöblom - 09 Mar 2004 20:56 GMT
> Hi,
>
[quoted text clipped - 26 lines]
> Thanks in advance,
> Ben.

I'll propose this:

import java.util.Arrays;

public static String removeChars(String str, char[] chars)
{
    StringBuffer buff = new StringBuffer(str.length());
    Arrays.sort(chars);

    for (int i=0, len = str.length(); i < len; i++)
    {
        char c = str.charAt(i);
        if (Arrays.binarySearch(chars, c) >= 0)
        {
            buff.append(c);
        }
    }
   
    return buff.toString();
}

       

Signature

Daniel Sjöblom
Remove _NOSPAM to reply by mail

Daniel Sjöblom - 09 Mar 2004 21:15 GMT
>> Hi,
>>
[quoted text clipped - 43 lines]
>         char c = str.charAt(i);
>         if (Arrays.binarySearch(chars, c) >= 0)

Oops! should be < 0

>         {
>             buff.append(c);
[quoted text clipped - 5 lines]
>
>        

Signature

Daniel Sjöblom
Remove _NOSPAM to reply by mail



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.