How to remove backslash("\") from string having ASCII string combination in java - java

Input string is "ABC\1067546161"
I want to remove "\" backslash and get digits only from the string but we are getting string with ascii value.Result String is ABCF7546161 after print.
Please suggest some solution.
Input String is ABC\1067546161
Expected result is 1067546161

May be something like this
"ABC\1067546161".replaceAll("[a-zA-Z\\]", "")

I think this could work, but the code is ugly as hell..
String word = "ABC\1067546161";
char badChar = word.charAt(3);
String[] arr = word.split(Character.toString(badChar));
System.out.println(Integer.toOctalString(badChar) + arr[1]);
You only mentioned one string in the question, but on several cases, this would most likely not work.

As #TheLostMind pointed out in a comment, you can't replace the backslash directly because the String is created with that value.
The only way to do that is manipulate the input itself and convert it into a byte array instead of a String. Then you can call the String constructor that takes a byte[] as argument and it won't be converted.
Once you have that, you can use a regex to remove the part you don't want as others suggested. Here's the code I've used to test this:
public static void main(String[] args) {
// Input manipulation.
byte[] input = {'A','B','C','\\','1','0','6','7','5','4','6','1','6','1'};
String string = new String(input);
System.out.println(string);
// Splitting.
String[] result = string.split("\\\\");
System.out.println(result[1]);
}

Related

I want to split a string with multiple whitespaces using split() method?

This program is to return the readable string for the given morse code.
class MorseCode{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String morseCode = scanner.nextLine();
System.out.println(getMorse(morseCode));
}
private static String getMorse(String morseCode){
StringBuilder res = new StringBuilder();
String characters = new String(morseCode);
String[] charactersArray = characters.split(" "); /*this method isn't
working for
splitting what
should I do*/
for(String charac : charactersArray)
res.append(get(charac)); /*this will return a string for the
corresponding string and it will
appended*/
return res.toString();
}
Can you people suggest a way to split up the string with multiple whitespaces. And can you give me some example for some other split operations.
Could you please share here the example of source string and the result?
Sharing this will help to understand the root cause.
By the way this code just works fine
String source = "a b c d";
String[] result = source.split(" ");
for (String s : result) {
System.out.println(s);
}
The code above prints out:
a
b
c
d
First, that method will only work if you have a specific number of spaces that you want to split by. You must also make sure that the argument on the split method is equal to the number of spaces you want to split by.
If, however, you want to split by any number of spaces, a smart way to do that would be trimming the string first (that removes all trailing whitespace), and then splitting by a single space:
charactersArray = characters.trim().split(" ");
Also, I don't understand the point of creating the characters string. Strings are immutable so there's nothing wrong with doing String characters = morseCode. Even then, I don't see the point of the new string. Why not just name your parameter characters and be done with it?

Java convert String to Unicode character. "U+1F600" = 😀

Please note that this question is not a duplicate.
I have a String like this:
// My String
String myString = "U+1F600";
// A method to convert the String to a real character
String unicodeCharacter = convertStringToUnicode(myString);
// Then this should print: 😀
System.out.println(unicodeCharacter);
How can I convert this String to the unicode character 😀? I then want to show this in a TextView.
What you are trying to do is to print the unicode when you know the code but as String...
the normal way to do this is using the method
Character.toChars(int)
like:
System.out.print(Character.toChars(0x1f600));
now in you case, you have
String myString = "U+1F600";
so you can truncate the string removing the 1st 2 chars, and then parsing the rest as an integer with the method Integer.parseInt(valAsString, radix)
Example:
String myString = "U+1F600";
System.out.print(Character.toChars(Integer.parseInt(myString.substring(2), 16)));
Try
yourTextVIew.setText(new String(Character.toChars(0x1F60B)));

Replace String with character code in Java

Can't figure out how to replace a string with a character in Java. I wanted a function like this:
replace(string, char)
but there's not a function like this in Java.
String str = str.replace("word", '\u0082');
String str = str.replace("word", (char)130);
How do I go about this?
Use a string as the replacement that happens to be only a single character in length:
String original = "words";
String replaced = original.replace("word", "\u0130");
The replaced instance will be equivalent to "Ä°s".
Note also that, from your question, '\u0130' and (char)130 are not the same characters. The \u syntax uses hexadecimal and your cast is using decimal notation.
Very simply:
String orginal = "asdf";
char replacement = 'z';
orginal = original.replace(original, replacement+"");
You asked for a "function" in Java, you can allways create a method like this
public static String replace (String text, char old, char n){
return text.replace(old, n);
}
Then you can call this method as you want
String a = replace("ae", 'e', '3');
In this case the method will return a String with a3 as value, but you can replace not only a char by another, you can replace a String with multiple characters in the same way
public static String replace (String text, String old, String n){
return text.replace(old, n);
}
Then you call this method like this
String a = replace("aes", "es", "rmy");
The result will be a String with army as value
the simplest way:
"value_".replace("_",String.valueOf((char)58 )); //return "value:"
EDIT:
(char)58 //return: ':'
(int)':' //return: 58
Since we want to work with codes and no character we have to pass code to character
another problem to solve is that method "replace" does not take "(String x,char y)"
So we pass our character to String this way:
String.valueOf((char)58) //this is a String like this ":"
Finally we have String from int code, to be replaced.
"String is a sequence of characters"
String s="hello";
char c='Y';
System.out.println(s.replace(s, Character.toString(c)));
Output:
Y

How to get specific string out of long string

I have the following String (it is variable, but classpath is always the same):
C:.Users.mho.Desktop.Eclipse.workspace.GIT.BLUBB...bin.de.test.class.mho.communication.InterfaceXmlHandler
and I want to get just
de.test.class.mho.communication.InterfaceXmlHandler
out of this string. The end
InterfaceXmlHandler
is variable, also the beginning before 'de' and the path itself is variable too, but
de.test.class.mho.
isn't variable.
Why not just use
String result = str.substring(str.lastIndexOf("de.test.class.mho."));
Instead of splitting you could get rid of the beginning of the string:
String input = "C:.Users.mho.Desktop.Eclipse.workspace.GIT.BLUBB...bin.de.test.class.mho.communication.InterfaceXmlHandler";
String output = input.replaceAll(".*(de\\.test\\.class\\.mho.*)", "$1");
You can create a string-array with String.split("de.test.class.mho."). The Array will contain two Strings, the second String will be what you want.
String longString = ""; //whatever
String[] urlArr = longString.split("de.test.class.mho.");
String result;
if(urlArr.length > 1) {
result = "de.test.class.mho." urlArr[1]; //de.test.class.mho.whatever.whatever.whatever
}
You can use replaceAll() to "extract" the part you want:
String part = str.replaceAll(".*(?=de\\.test\\.class\\.mho\\.)", "");
This uses a look-ahead to find all characters before the target, and replace them with a blank (ie delete them).
You could quite reasonably ignore escaping the dots for brevity:
String part = str.replaceAll(".*(?=de.test.class.mho.)", "");
I doubt it would give a different result.

Java replaceAll method with a variable string and escaped dot

I'm having a hard time figuring this one out, so I ask for your help. Here's the deal:
String str = "02-EST-WHATEVER-099-00.dwg";
String newStr = str.replaceAll("([^-_\\.]+-[^-_\\.]+-[^-_\\.]+-[^-_\\.]+-)[^-_\\.]+(\\.[^-_\\.]+)", "$1$2");
The block of code above results in 02-EST-WHATEVER-099-.dwg (removed the last "00", just before the extension). Great, that's what I need!
But the RegEx I use above has to be created on the fly (the field I'm removing can be in a different position). So I used some code to create the RegEx string (here's what the result would look like if I just declared it):
String regexRemoveRev = "([^-_\\.]+-[^-_\\.]+-[^-_\\.]+-[^-_\\.]+-)[^-_\\.]+(\\.[^-_\\.]+)";
Now, if I out.print(regexRemoveRev), I get ([^-_\.]+-[^-_\.]+-[^-_\.]+-[^-_\.]+-)[^-_\.]+(\.[^-_\.]+) (notice the single backslashes).
And when i try the replaceAll again, it doesn't work:
String str = "02-EST-WHATEVER-099-00.dwg";
String newStr = str.replaceAll(regexRemoveRev, "$1$2");
So I thought it could be because of the single backslashes, and I tried declaring regexRemoveRev with 4 of them, instead of just 2:
String regexRemoveRev = "([^-_\\\\.]+-[^-_\\\\.]+-[^-_\\\\.]+-[^-_\\\\.]+-)[^-_\\\\.]+(\\\\.[^-_\\\\.]+)";
The output of out.print(regexRemoveRev) is the double backslash version of the RegEx, as expected:
([^-_\\.]+-[^-_\\.]+-[^-_\\.]+-[^-_\\.]+-)[^-_\\.]+(\\.[^-_\\.]+)
But the replace still doesn't work!
How do I get this to do what I want?
I have just wrote a short program and in both cases it works here it is:
public class StringTest
{
public static void main(String[] args)
{
String str = "02-EST-WHATEVER-099-00.dwg";
String newStr = str.replaceAll("([^-_\\.]+-[^-_\\.]+-[^-_\\.]+-[^-_\\.]+-)[^-_\\.]+(\\.[^-_\\.]+)", "$1$2");
String regexRemoveRev = "([^-_\\.]+-[^-_\\.]+-[^-_\\.]+-[^-_\\.]+-)[^-_\\.]+(\\.[^-_\\.]+)";
String newStr1 = str.replaceAll(regexRemoveRev, "$1$2");
System.out.println("newStr: "+newStr);
System.out.println("regexRemoveRev: "+regexRemoveRev);
System.out.println("newStr: "+newStr1);
}
}
The out put from the above:
newStr: 02-EST-WHATEVER-099-.dwg
regexRemoveRev: ([^-.]+-[^-.]+-[^-.]+-[^-.]+-)[^-.]+(.[^-.]+)
newStr: 02-EST-WHATEVER-099-.dwg
I am not sure why is not working for you!! or is it something else you are asking and I got wrong

Categories