Avatar of David King
David KingFlag for United States of America

asked on 

Java String Exercise

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
JavaRegular Expressions

Avatar of undefined
Last Comment
David King
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Some pointers:

String.toUpperCase
String.replace
StringBuilder.reverse
Avatar of David King
David King
Flag of United States of America image

ASKER

Thanks, I'll research the API docs on those.
Avatar of David King
David King
Flag of United States of America image

ASKER

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?
?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of David King
David King
Flag of United States of America image

ASKER

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. ??
ASKER CERTIFIED SOLUTION
Avatar of David King
David King
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of awking00
awking00
Flag of United States of America image

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.
Avatar of awking00
awking00
Flag of United States of America image

Too late with my replaceAll suggestion :-(
Avatar of awking00
awking00
Flag of United States of America image

Now you just need to create a string that's the reverse of the string you have and compare them to complete your task.
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

When you come back again with your next question, be great if you could stick your code in the code formatting delimiters. ;)
Avatar of David King
David King
Flag of United States of America image

ASKER

Figured it out.
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo