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)));
Related
I am storing below number as a string. How can I remove the first three characters & last one character from the string?
String No = "00098000002208";
The easiest and probably fastest way would be to just use the base string function substring:
String input = "00034000004409";
String output = input.substring(3, input.length()-1);
We could also try doing a regex replacement:
String output = input.replaceAll("^.{3}(.*).$", "$1");
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]);
}
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
I have a string like this :
12
I want to get split to [2] ignoring 1. Is it possible to do so in java?
You can use the split() method to split on a regex input or, better yet, if you know the exact position or character you want to split at (as seems to be the case here), just use substring() combined with indexOf(). Something like:
String substring = string.substring(0, indexOf("2"));
where string is your original String variable..
If you know the exact index,
String str = "12"
String s = str.substring(1,2); // output 2
or
String s = str.substring(0, indexOf("2")); //output 2
or
char[] chars = str.toCharArray();
char c = chars[1]; // output
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.