Java String Exercise

David King
David King used Ask the Experts™
on
I am working on an exercise in JAVA 1.8 that goes like this.
I have a String : "Able was I, ere I saw Elba."
I need to remove spaces and punctuation and capitalize it all.
Then I need to save a copy of it in reverse order and compare the two to see if they are the same.

Does anyone have a suggestion on how they might achieve this ?
I was thinking along the lines of a regex /[a-zA-Z]+/g    as a possibility. But I don't know how to extract the words using a RegEx.
The result of the extraction would have the above expression returned as "ABLEWASIEREISAWELBA" .

Thanks,

Dave
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
Some pointers:

String.toUpperCase
String.replace
StringBuilder.reverse
David KingDirector I.T. Systems

Author

Commented:
Thanks, I'll research the API docs on those.
David KingDirector I.T. Systems

Author

Commented:
I have tested my RegEx /[a-zA-Z]+/g on my test string and it selects all of the words correctly. Is there a String class method that I can use that will allow me to use my RegEx expression to select the Words in my String and Return them back as a string, leaving the un-selected text behind?
?
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Top Expert 2016

Commented:
I have tested my RegEx /[a-zA-Z]+/g on my test string and it selects all of the words correctly.
How and where?
Is there a String class method that I can use that will allow me to use my RegEx expression to select the Words in my String and Return them back as a string
That's not really how regex works. You need to think about replacement
David KingDirector I.T. Systems

Author

Commented:
I am trying to use the String.replace(regex , string) method to get the job done.

Like this;

public class PalCode
{

    public PalCode()
    {
        String palString = new String("Able was I, ere I saw Elba."); // original input String
        String workedString = new String(""); // String after manipulation
        workedString = palString.replace("[^a-zA-Z]+/g" , "");
        System.out.println("Manipulated string result is: " + workedString);

    }
}// end class PalCode

The regex expression should match character that are NOT a-z or A-Z and replace them with "nothing".
The program runs and compiles without any errors but the regex has NO effect. ??
Director I.T. Systems
Commented:
OK, I got it. This works.

public class PalCode
{

    public PalCode()
    {
        String palString = new String("Able was I, ere I saw Elba."); // original input String
        String workedString = new String(""); // String after manipulation
        workedString = palString.replaceAll("[^a-zA-Z]+" , "").toUpperCase();
        System.out.println("Manipulated string result is: " + workedString);

    }
}// end class PalCode


("Able was I, ere I saw Elba."); // original input String
Manipulated string result is: ABLEWASIEREISAWELBA
awking00Information Technology Specialist

Commented:
The String replace method will replace a char sequence or array. I think you should look at the replaceAll method which takes a regular expression as its first argument.
awking00Information Technology Specialist

Commented:
Too late with my replaceAll suggestion :-(
awking00Information Technology Specialist

Commented:
Now you just need to create a string that's the reverse of the string you have and compare them to complete your task.
When you come back again with your next question, be great if you could stick your code in the code formatting delimiters. ;)
David KingDirector I.T. Systems

Author

Commented:
Figured it out.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial