I am trying to write a code to find the special characters in a java string.
Special characters are a-zA-Z.?#;'#~!£$%^&*()_+-=¬`,./<>
Please help me to understand and write how can I implement this.
Thank you
You can create a char array from a String.
String string = "test";
char[] charArray = string.toCharArray();
Then you can loop through all the characters:
for(char character: charArray){
//check if the character is special and do something with it, like adding it to an List.
}
You can use a Scanner to find the invalid characters in your String:
/* Regex with all character considered non-special */
public static final String REGULAR_CHARACTERS = "0-9a-z";
public static String specialCharacters(String string) {
Scanner scanner = new Scanner(string);
String specialCharacters= scanner.findInLine("[^" + REGULAR_CHARACTERS + "]+");
scanner.close();
return specialCharacters;
}
The findInLine returns a String with all characters not included in the constant (all special characters). You need to setup the constant with all the characters that you consider non-special.
Alternatively, if you want to setup only the characters you want to find, you can modify the example above with:
public static final String SPECIAL_CHARACTERS = "a-zA-Z.?#;'#~!£$%^&*()_+-=¬`,./<>";
....
String specialCharacters= scanner.findInLine("[" + SPECIAL_CHARACTERS + "]+");
....
The characters used in the constants need to be scaped as usual for regular expressions.
For example, to add the ] character, you need to use \\]
Related
So I am I wrote a method to replace all char's in a string, and it works. However if the letter I want to replace is sandwiched in a word, there is a space in place of the replaced letter, so the one word is now two. Here is an example
String str is Hello World . The character I want to make blank is o . My code will return this string Hell W rld. How do I change my method so it outputs Hell Wrld
Here is my code
public static void main(String[] args) {
System.out.println(removeAll("Hello World", 'e'));
}
public static String removeAll(String str, char letter) {
String replace = str.replace(letter,' ');
return replace;
}
You are replacing passed character with space in your replace method.
Replace char with CharSequence version lile str = str.replace("X", "");
"X" here is just an example you can pass your char and convert to string and use it
You can use:
str = str.replace("o", "");
Replace your intended letter with empty String.
Change
String replace = str.replace(letter,' ');
to
String replace = str.replace(letter,"");
Otherwise, you are putting a white space where 'o' used to be. If you want to completely burn that character, replace it with an empty character.
I'm trying to replace all characters between two delimiters with another character using regex. The replacement should have the same length as the removed string.
String string1 = "any prefix [tag=foo]bar[/tag] any suffix";
String string2 = "any prefix [tag=foo]longerbar[/tag] any suffix";
String output1 = string1.replaceAll(???, "*");
String output2 = string2.replaceAll(???, "*");
The expected outputs would be:
output1: "any prefix [tag=foo]***[/tag] any suffix"
output2: "any prefix [tag=foo]*********[/tag] any suffix"
I've tried "\\\\\[tag=.\*?](.\*?)\\\\[/tag]" but this replaces the whole sequence with a single "\*".
I think that "(.\*?)" is the problem here because it captures everything at once.
How would I write something that replaces every character separately?
you can use the regex
\w(?=\w*?\[)
which would match all characters before a "[\"
see the regex demo, online compiler demo
You can capture the chars inside, one by one and replace them by * :
public static String replaceByStar(String str) {
String pattern = "(.*\\[tag=.*\\].*)\\w(.*\\[\\/tag\\].*)";
while (str.matches(pattern)) {
str = str.replaceAll(pattern, "$1*$2");
}
return str;
}
Use like this it will print your tx2 expected outputs :
public static void main(String[] args) {
System.out.println(replaceByStar("any prefix [tag=foo]bar[/tag] any suffix"));
System.out.println(replaceByStar("any prefix [tag=foo]loooongerbar[/tag] any suffix"));
}
So the pattern "(.*\\[tag=.*\\].*)\\w(.*\\[\\/tag\\].*)" :
(.*\\[tag=.*\\].*) capture the beginning, with eventually some char in the middle
\\w is for the char you want to replace
(.*\\[\\/tag\\].*) capture the end, with eventually some char in the middle
The substitution $1*$2:
The pattern is (text$1)oneChar(text$2) and it will replace by (text$1)*(text$2)
I am having below String value, in that how can I find the only this four specified special character like [],:,{},-() (square bracket, curly bracket, hyphen and colon) in a given String.
String str = "[1-10],{10-20},dhoni:kholi";
Kindly help me as I am new to Java.
I think you can use regular expression like this.
class MyRegex
{
public static void main (String[] args) throws java.lang.Exception
{
String str = "[1-10],{10-20},dhoni:kholi";
String text = str.replaceAll("[a-zA-Z0-9]",""); // replacing all numbers and alphabets with ""
System.out.print(text); // result string
}
}
Hope this will help you.
If it is only characters that you want to check then you can use String.replaceAll method with regular expression
System.out.println("[Hello {}:-,World]".replaceAll("[^\\]\\[:\\-{}]", ""));
Need some help, I need to remove white space and punctuation from user input word. Then sort it by rearranging the letters(no specific way).
my Problem is that i can either remove punctuation and spaces or I can sort the characters but i cant get it to do both. it just prints out the hex. Any help please.
public class Assignment6_main {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a string: ");
String s = input.nextLine();
sort(s);
}
public static void sort(String myString) {
char[] chars = myString.toCharArray();
//sort characters
Arrays.sort(chars);
//remove punctuation and white space
String noPuncs = myString.replaceAll("\\W", "");
System.out.println(noPuncs + chars);
}
}
You nearly had it. The \W looked like it needed to be escaped twice, and if you sort the String that had the characters removed, you could just print that:
public static void sort(String myString) {
String noPuncs = myString.replaceAll("\\W", "");
char[] chars = noPuncs.toCharArray();
Arrays.sort(chars);
System.out.println(chars);
} }
You need to manually convert the character array back to a string:
System.out.println(noPuncs + new String(chars));
When you do a plain
System.out.println(chars);
You are calling the println(char x[]), which is clever about making things look nice, but when you concatenate it with a String, you will call toString() on a primitive array, which is just going to give you an object reference.
I'm think this what you want to do. You replace all the whitespace of the string FIRST, then you turn it into a char array and sort it. Also, "noPuncs" really isn't what this string is, it should be "noWhitespace". You haven't removed any punctuation as of yet.
public static void sort(String myString) {
String noWhitespace = myString.replaceAll("\\W", "");
char[] chars = noWhitespace.toCharArray();
//sort characters
Arrays.sort(chars);
//remove punctuation and white space
System.out.println(noWhitespace + new String(chars));
}
Regarding the punctuation, you might find some interesting informations from this question in stackoverflow
I want to split a file with a pipe character on a string like number|twitter|abc.. in the mapper.
It is a long string. But it doesn't recognize pipe delimiter when I do:
String[] columnArray = line.split("|");
If I try to split it with a space like line.split(" "), it works fine so I don't think there is a problem with it recognizing characters.
Is there any other character that can look like pipe? Why doesn't split recognize the | character?
As shared in another answer
"String.split expects a regular expression argument. An unescaped | is parsed as a regex meaning "empty string or empty string," which isn't what you mean."
https://stackoverflow.com/a/9808719/2623158
Here's a test example.
public class Test
{
public static void main(String[] args)
{
String str = "test|pipe|delimeter";
String [] tmpAr = str.split("\\|");
for(String s : tmpAr)
{
System.out.println(s);
}
}
}
String.split takes a regular expression (as the javadoc states), and "|" is a special character in regular expressions. try "[|]" instead.