David King
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
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
ASKER
Thanks, I'll research the API docs on those.
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?
?
?
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 stringThat's not really how regex works. You need to think about replacement
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("Manipu lated 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. ??
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-
System.out.println("Manipu
}
}// 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
Too late with my replaceAll suggestion :-(
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. ;)
ASKER
Figured it out.
String.toUpperCase
String.replace
StringBuilder.reverse