Java: removing multiple occurrences of unprintable characters from group [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 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", "")));

Related

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;

What is the difference between a String[] and String in a for loop (Java) [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 5 years ago.
Improve this question
Before you ask, yes I did Google this first. I haven't found a proper answer yet. I understand the syntax for a String array in a for loop, but not for a String. For example, let's say I have a fragment code which includes a for loop that's purpose is to adjust an element of the String to "josh" if that element isn't equal to something (I can't think of anything at the top of my head). The fragment code would be like this:
public void adjustScore(String[] str){
for(int j= 0; j < str.length; j++){
if(str[j] != //idk, something//
str[j]= "josh";
}
else{};
}
But, how would this look if it was a String instead of a String[]?
public void adjustScore(String str2){
for(int j=0; j < str2.length(); j++){
// How do I call an element from the String? Would I still use str2[j]?//
In Java, a String is not an array of characters. Although it is true that the only "elements" in a String are characters, you can use String.charAt(int) to get a character at a valid index (but you cannot use []).
char ch = str2.charAt(j);

Remove non-standard characters from a String in java [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 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)

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.*");

Find all substring contain a keyword [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 9 years ago.
Improve this question
I want to find all the substring of one String that contains a key word.
Ex: "This is the keyword in the string".
Output: the keyword, this is the keyword, the keyword in the string, is the keyword in ....
I am think of finding all the substrings first then try to filter one by one. But I think that would be very bad solution.
Could you please give me some advice to do that!. Thank you very much.
I have edited to just find the sequence of tokens.
Try this:
String str = "abcdefkeybncv...";
String key = "key";
int index = str.indexOf(key);
ArrayList<String> sub = new ArrayList<String>();
for (int i = 0; i < str.length(); i++) {
for (int j = 0; j <= str.length() - i; j++) {
String s = str.substring(i, i+j);
if(s.indexOf(key) >= 0){
sub.add(s);
}
}
}
System.out.println(sub);
Output for the code above:
[abcdefkey, abcdefkeyb, abcdefkeybn, abcdefkeybnc, abcdefkeybncv, abcdefkeybncv., abcdefkeybncv.., abcdefkeybncv..., bcdefkey, bcdefkeyb, bcdefkeybn, bcdefkeybnc, bcdefkeybncv, bcdefkeybncv., bcdefkeybncv.., bcdefkeybncv..., cdefkey, cdefkeyb, cdefkeybn, cdefkeybnc, cdefkeybncv, cdefkeybncv., cdefkeybncv.., cdefkeybncv..., defkey, defkeyb, defkeybn, defkeybnc, defkeybncv, defkeybncv., defkeybncv.., defkeybncv..., efkey, efkeyb, efkeybn, efkeybnc, efkeybncv, efkeybncv., efkeybncv.., efkeybncv..., fkey, fkeyb, fkeybn, fkeybnc, fkeybncv, fkeybncv., fkeybncv.., fkeybncv..., key, keyb, keybn, keybnc, keybncv, keybncv., keybncv.., keybncv...]
Build suffix array: http://en.wikipedia.org/wiki/Suffix_array
Use binary search to find your substring there
Move up and down from this point in suffix array while suffixes starts with substring

Categories

Resources