The problem is your changes are not being stored because Strings are immutable. Each of the method calls is returning a new String representing the change, with the current String staying the same. You just need to store the returned String back into the array.
line[i] = line[i].replaceAll("[^a-zA-Z]", "");
line[i] = line[i].toLowerCase();
Because the each method is returning a String you can chain your method calls together. This will perform the second method call on the result of the first, allowing you to do both actions in one line.
line[i] = line[i].replaceAll("[^a-zA-Z]", "").toLowerCase();
Answer from n00begon on Stack OverflowThe problem is your changes are not being stored because Strings are immutable. Each of the method calls is returning a new String representing the change, with the current String staying the same. You just need to store the returned String back into the array.
line[i] = line[i].replaceAll("[^a-zA-Z]", "");
line[i] = line[i].toLowerCase();
Because the each method is returning a String you can chain your method calls together. This will perform the second method call on the result of the first, allowing you to do both actions in one line.
line[i] = line[i].replaceAll("[^a-zA-Z]", "").toLowerCase();
You need to assign the result of your regex back to lines[i].
for ( int i = 0; i < line.length; i++) {
line[i] = line[i].replaceAll("[^a-zA-Z]", "").toLowerCase();
}
How can I remove all Non-Alphabetic characters from a String using Regex in Java - Stack Overflow
java - Replacing all non-alphanumeric characters with empty strings - Stack Overflow
6.19 LAB: Remove all non-alphabetic characters Write a program that removes all non-alphabetic characters from the given input. Ex: If the input is: -Hello, 1 worlds! the output is: Helloworld Your program must define and call the following method. The method should return a string representing the input string without non- alphabetic characters public
Trying to remove non-alphabetical characters from inputted string need help pls
Take a look replaceAll(), which expects a regular expression as the first argument and a replacement-string as a second:
return userString.replaceAll("[^\\p{Alpha}]", "");
for more information on regular expressions take a look at this tutorial
The issue is that your regex pattern is matching more than just letters, but also matching numbers and the underscore character, as that is what \W does. Replacing this fixes the issue:
String[] stringArray = userString.split("\\P{Alpha}+");
Per the Pattern Javadocs, \W matches any non-word character, where a word character is defined in \w as [a-zA-Z_0-9]. This means that it matches upper and lowercase ASCII letters A - Z & a - z, the numbers 0 - 9, and the underscore character ("_").
The solution would be to use a regex pattern that excludes only the characters you want excluded. Per the pattern documentation could do [^a-zA-Z] or \P{Alpha} to exclude the main 26 upper and lowercase letters. If you want to count letters other than just the 26 ASCII letters (e.g. letters in non-Latin alphabets), you could use \P{IsAlphabetic}.
\p{prop}matches if the input has the property prop, while\P{prop}does not match if the input has that property.
As other answers have pointed out, there are other issues with your code that make it non-idiomatic, but those aren't affecting the correctness of your solution.
Hey guys! For class I'm trying to make a program to remove all non-alpha characters from a inputted string. Can you tell me why this isn't working?
For some reason when I input for example "testing test ! bla -o-" it outputs "testingtest!bla-o"
#include <iostream>using namespace std;int main() {
string inputStr;getline(cin, inputStr);for (int i = 0; i < inputStr.size(); i++){if (!isalpha(inputStr[i])){inputStr.replace(i, 1, "");}}cout << inputStr;return 0;}