Remove non-standard characters from a String in java [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a string that contains a lot of text. There's some weird characters in it like the following: █ ✖ ✔ ♫ ♬ ▬ ★
This is just a small portion of what I have found so far. I tried using the replaceAll method but it doesn't seem to work. Is there a collection of all these types of characters somewhere, or even better yet, a library that is able to remove them?

Iterate over characters and check each whether it belongs to some category you define as "standard" (here such categories are: alphabetic, digit, whitespace, or modifier applied to previously accepted character):
static String standartize(String s) {
if (s == null) return null;
StringBuilder sb = new StringBuilder();
boolean based = false; // is previous character accepted base for modifier?
int c;
for (int i = 0; i < s.length(); i += Character.charCount(c)) {
c = Character.codePointAt(s, i);
if (based && Character.getType(c) == Character.MODIFIER_SYMBOL) {
sb.appendCodePoint(c);
} else if (Character.isAlphabetic(c) || Character.isDigit(c)) {
sb.appendCodePoint(c);
based = true;
} else if (Character.isWhitespace(c)) {
sb.appendCodePoint(c);
based = false;
} else {
based = false;
}
}
return sb.toString();
}
You can add/remove checks in else if to widen/narrow range of characters you consider "standard": Character has many static isXxxx() methods to test if a character belongs to some category.
Please notice that iterated are not char items, but int codepoints. This is made to process not only UTF-16 chars, but surrogate pairs as well.

If you want only ASCII Characters in your string, you can loop through the length of the string and check wether ASCII value is between 65 - 90(A-Z) or 97 - 122(a-z) or 48-57(0 - 9)

Related

Turn duplicates into distinct letters in a String [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
We will be given a string s. Suppose for example "creepfe". What we want to do is to remove duplicate and instead add a new letter in that place and new letter must be distinct letter available next to the duplicate letter in alphabetical order. So it goes like this :
creepfe to crefpfe -- first e is distinct and second e is changed to f which is distinct upto that.
crefpfe to crefpge -- second f is changed to g since we already have f before.
crefpge to crefpgf -- since e is already present.
Now again we got f duplicate , so change it to crefpgg , which again got g duplicate so finally we reach "crefpgh" which has all distinct letters.
Started learning java recently and a working code is appreciated ,BUT a good algorithm is what really needed.
Edit : yes capitals do count as duplicates as well. And string length is limited to 10-15 so no worry about running out of distinct letter.
Here's a solution. I m using recursion to left shift the letters if there are duplicates. I also went back and redid my code to include sets as mentioned by MBo. Its not the most efficient, but its a start while you wait for advice from more experienced members of SO
public class tester {
public static void main(String[] args){
//Application.launch(testclass.class, args);
String str = "creepFeZZ";
System.out.println(processStr(str));
}
public static String processStr(String str){
StringBuilder sb = new StringBuilder();
HashSet<String> seen = new HashSet<>();
insertStr(sb, seen, String.valueOf(str.charAt(0)));
for (int i=1; i<str.length(); i++){
char currentchar = str.charAt(i);
char processedchar = goNext(seen, currentchar);
insertStr(sb, seen, String.valueOf(processedchar));
}
//System.out.println(seen.toString());
return sb.toString();
}
private static void insertStr(StringBuilder sb, HashSet seen, String str){
seen.add(str.toLowerCase());
sb.append(str);
}
private static char goNext(HashSet seen, char c){
if (c>= 65 && c<=90){
//if uppercase letter, check if contains lowercase version
if (seen.contains(String.valueOf((char)(c+32)))){
c = goNext(seen, (char)(c+1));
}
//any left shifting will overflow back to A
return (char) ((c -(int) 'A') % 26 +(int) 'A');
}else{
//if lowercase letter, just check if contains
if (seen.contains(String.valueOf((char)(c)))){
c = goNext(seen, (char)(c+1));
}
//any left shifting will overflow back to a
return (char)((c-(int) 'a') % 26 +(int) 'a');
}
}
}
This gives output of:
crefpGhZA
Find the position where the string contains a duplicate. There are various methods to this. You can Google to find the most efficient one that fits your approach.
Generate the next character in alphabetical order. The following code shows how this can be done
String value = "C";
int charValue = value.charAt(0);
String next = String.valueOf( (char) (charValue + 1));
System.out.println(next);
Repeat the process until there are no more duplicates (have a while loop which breaks when there are no more duplicates)

I want to change the string of letters xxx-xxx-xxxx into 333-333-3333 I tried this but getting an error in the if(phoneNumber.charAt(0)) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
public class ABCTelephoneTranslator {
public static String translate(String phoneNumber) {
// Creating array of string length
char[] ch = new char[phoneNumber.length()];
// Copy character by character into array
for (int i = 0; i < phoneNumber.length(); i++) {
ch[i] = phoneNumber.charAt(i);
}
// Printing content of array
for (char c : ch) {
System.out.println(c);
}
System.out.println(ch.length);
if(ch.length > 12) {
throw new IllegalArgumentException("the format should be XXX-XXX-XXXX");
}
if(phoneNumber.charAt(0) == "x") {
phoneNumber.charAt(0) = "3";
}
return phoneNumber;
}
I need to change the string of letters xxx-xxx-xxxx into 333-333-3333 how do i do that I tried this but getting an error where it says if(phoneNumber.charat(0) == "x") and below that please help me resolve this thank you
There are easier ways to do this but to answer your specific question you need to do the following. At the end of your method:
for (int i = 0; i < ch.length; i++) {
if(ch[i] == 'x' { // single quotes.
ch[i] = '3';
}
}
return new String(ch);
Also, to get the array of characters you can do.
char ch[] = phonenumber.toCharArray();
And contrary to what others have told you,
phonenumber.replace("x","3");
Doesn't work because Strings are immutable. You need to reassign it.
phonenumber = phonenumber.replace("x","3");
charAt functionr returns a character. You cannot use it to compare it to a string. You need to compare it to a character. So, your if-condition should be phoneNumber.charAt(0) == 'x'
It would help you to go through the Java Documentation
Try using "phoneNumber.replace("x","3");" It should replace the 'X's with '3's

Java - Event handle data protection for Strings with 2 data types [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
For event handling how can you check a string that has a combination of integers and a character?
For example - 1234p
If the user enters the example above, how can you check if the user enters integers first and then a character at the end? What kind of exception will be thrown if the data type input is not an integer or char?
You can use REGEX [0-9]+[a-zA-Z] to match if the string contains chars and integers otherwise throw an IllegalArgumentException
public void check(String input) {
if (!input.matches("[0-9]+[a-zA-Z]")) {
throw IllegalArgumentException("Not valid string");
}
// do other logic
}
So from your question it sounds like you expect a string that has all numbers except the last character that has to be an alphabet. You can check if the given string matches this condition the following way too:
String string = "1234p";
int length = string.length();
boolean numsFirst = string.substring(0, length - 1).chars().allMatch(x -> Character.isDigit(x));
boolean lastChar = Character.isDigit(string.charAt(length - 1));
if(numsFirst && lastChar)
return true;
else
return false;

Java: removing multiple occurrences of unprintable characters from group [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
In out application we need to compare and manipulate functions code like the following:
function void disableTestOnDevice(SampleBuilder sample,String devices,String[] devTestCode) {\r\n\t\t if(devices == null)\r\n\t\t\t return;\r\n\t if(sample.getListTransc() != null && sample.getListTransc().size() > 0){\t\r\n\t\t\t\tfor(int i = sample.getListTransc().size()-1; i >= 0; i--){\r\n\t\t\t\t\tViewWorklistBuilder j = (ViewWorklistBuilder)sample.getListTransc().get(i);\r\n\t\t\t\t\tif(j.getDeviceId() == null || j.getDeviceTestCode() == null || j.getDeviceSpecimenCode() == null){\r\n\t\t\t\t\t\tcontinue;\r\n\t\t\t\t\t}\r\n\t\t\t\t\tif(j.getDeviceId().equals(devices)){\r\n\t\t\t\t\t\tif(devTestCode != null){\r\n\t\t\t\t\t\t\tfor(int k = 0; k < devTestCode.length; k++){\r\n\t\t\t\t\t\t\t\tif(j.getDeviceTestCode().equals(devTestCode[k])){\r\n\t\t\t\t\t\t\t\t\tj.setEnabled(0);\r\n\t\t\t\t\t\t\t\t\tbreak;\r\n\t\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\t\t\t\t\t\t\r\n\t\t\t\t}\r\n\t }\r\n\t\r\n
Sometimes it happens that some extra \t, \r or \n is added, I don't know why.
So, before comparing two functions (two String objects) I would like to remove multiple occurrences of unprintable characters from each group.
For example, a group like
\r\n\t\r\n should become \t\r\n
Or maybe, something like
if(devices == null)\r\n\t\t\t return;\r\n\t\t
for(int i = sample.getListTransc().size()-1; i >= 0; i--){\t\r\n\t\t\t\tSystem.out.println(i);\t\r\n\t\t\t\}
should become
if(devices == null)\r\n\t return;\r\n\t
for(int i = sample.getListTransc().size()-1; i >= 0; i--){\t\r\nSystem.out.println(i);\t\r\n}
Or maybe, my problem could be a little more insidious.
I mean that it may be necessary that any group of unprintable characters matching \r\n\t should become \n\t
If the aim is to compare the change with another string, a possible solution is to remove \r, \n and \t in both and compare it.
String s = "function void disableTestOnDevice(SampleBuilder sample,String devices,String[] devTestCode) {\r\n\t\t if(devices == null)\r\n\t\t\t return;\r\n\t";
String s1 = "function void disableTestOnDevice(SampleBuilder sample,String devices,String[] devTestCode) {\n\t\t if(devices == null)\n\t\t\t return;\n\t";
System.out.println(s.replaceAll("\\r|\\n|\\t", ""));
// false
System.out.println(s.equals(s1));
// true
System.out.println(s.replaceAll("\\r|\\n|\\t", "").equals(s1.replaceAll("\\r|\\n|\\t", "")));

How to check if String has a Letter after a specific letter? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Example:
String letter = "abc+ab.c+abc.ab";
How to check if String letter contains . after last +?
About your modified question: to match . any time after + char try this:
Letter.matches(".*\\+.*\\..*");
Note: you it was necessary to escape + and . with \\ to match dot and plus literally, since those have a special meaning in regex.
Update: If you only want to match only if there is a dot after last + you could do this:
Letter.matches(".*\\+.*\\.[^+]*");
where the [^+] part means any char except +.
BTW: I would name your String letter instead of Letter because of naming conventions
Try this:
public boolean hasLetter(String string, char afterLetter, char letter) {
int index = string.indexOf(afterLetter);
if (index == -1 || index == string.length() -1) {
return false;
}
if (string.charAt(index+1) == letter) {
return true;
}
return false;
}
You could do something similar to:
for (int i = 0; i < letter.length() - 1; i++) {
if (letter.charAt(i) == '0' {
if (letter.charAt(i+1) == 'c' {
execute desired action here;
}
}
}
Try to use
Letter.matches(".*0c.*");

Categories

Resources