e.g.
B is uppercase B.
so if I have string like "BOY". I want it converted to BOY
I'm hoping there's already a library I can use. I've searched the net but I didn't see it.
thanks
Those codes are nothing but concatenation of &# and ; with the Unicode Codepoint for each character. You can iterate over each character in the string, and do:
output.append("&#")
.append((int)ch)
.append(";");
Where, output refers to a StringBuilder instance.
You could try writing your own utility:
String input = "BOY";
char[] chars = input.toCharArray();
StringBuilder output = new StringBuilder();
for (char c : chars)
{
output.append("&#").append((int) c).append(";");
}
output content after execution:
BOY
Related
I am writing a cryptography program that converts the ciphertext to plaintext. By the letter's frequency, I manually crack all the cipher's letters to plaintext, and there is no patterns, so I need to write a loop that contains 26 times, to convert every single letter in the string to another letter.
My first try is using the replace statement in java,
which will be like this
str=str.replaceAll("A","E");
The question is, for instance, I input a string like this abc
and my key will be like this "replace a with c, replace b with p, replace c with q".
The output I want should be cpq, but the output I got is qpq.
Which means I want it be like one time convert.
So I am trying to use the loop, using the if
The way I am doing it is:
for (i=1;i<string.length;i++)
if (char[i]=="a")
char [i] ="b";
The error tells me that I can't convert string to char.
And also, I want to ask if I put the other 25 if statements inside this statement or pararall them?
To answer the second part of your question: I would not add 25 single if statements in your loop but instead use a Map<Character, Character> to store your replacements.
A complete solution could look like this:
public static void main(String[] args) {
Map<Character, Character> replacements = new HashMap<>();
replacements.put('a', 'c');
replacements.put('b', 'p');
replacements.put('c', 'q');
String input = "abcd";
StringBuilder output = new StringBuilder();
for (Character c : input.toCharArray()) {
output.append(replacements.getOrDefault(c, c));
}
System.out.println(output.toString());
}
This will keep characters you don't have a replacement for. Output:
cpqd
You can solve this as :
String str = "abc";
char [] ch = str.toCharArray();
for(int i=0; i<str.length(); i++){
if('a'==str.charAt(i)){
ch[i] = 'c';
}
if('b'==str.charAt(i)){
ch[i] = 'p';
}
if('c'==str.charAt(i)){
ch[i] = 'q';
}
}
System.out.println(String.valueOf(ch));
You can't convert a String to a char. So you need to either use all Strings or all chars. Instead of using the double quotes for String you could use the single quotes for char like this:
char[i]=='b'
and
char[i] = 'b';
Also instead of using a long if/else statement you could either use a switch statement, or even better you could create and use a Map to store the conversions in key/value pairs.
Before posting I tried using string split("\u") or \\u or \u it does not work, reason being is that \u is considered as unicode character while in this case it's not.
as already mentioned \u12345 is a unicode character and therefore handled as a single symbol.
If you have these in your string its already too late. If you get this from a file or over network you could read your input and escape each \ or \u you encounter before storing it in your string variable and working on it.
if you elaborate the context of your task a little more, perhaps we could find other solutions for you.
Java understands it as Unicode Character so, right thing to do will be to update the source to read it properly and avoid passing Unicode to java if not needed. One workaround way could be to convert the entire string into a character Array and check if character is greater than 128 and if yes, I append the rest of the array in a seperate StringBuilder. See of it below helps :
public static void tryMee(String input)
{
StringBuilder b1 = new StringBuilder();
StringBuilder b2 = new StringBuilder();
boolean isUni = false;
for (char c : input.toCharArray())
{
if (c >= 128)
{
b2.append("\\u").append(String.format("%04X", (int) c));
isUni = true;
}
else if(isUni) b2.append(c);
else b1.append(c);
}
System.out.println("B1: "+b1);
System.out.println("B2: "+b2);
}
Try this. You did not escape properly
split("\\\\u")
or
split(Pattern.quote("\\u"))
import java.util.Arrays;
public class Example {
public static void main (String[]args){
String str = "abc\u12345";
// first replace \\u with something else, for example with -u
char [] chars = str.toCharArray();
StringBuilder sb = new StringBuilder();
for(char c: chars){
if(c >= 128){
sb.append("-u").append(Integer.toHexString(c | 0x10000).substring(1) );
}else{
sb.append(c);
}
}
String replaced = sb.toString();
// now you can split by -u
String [] splited = sb.toString().split("-u");
System.out.println(replaced);
System.out.println(Arrays.toString(splited));
}
}
As an example I have abcdbab and I want to replace all ab with A.
The output is AcdbA.
I try this one but it gives an error.
char N = 65;
String S = "abcdbab";
S = S.replaceAll("ab", N);
System.out.print(S);
Is there any method to do this?
Use String.replace(CharSequence,CharSequence) (remember String is immutable, so either use the result or assign it back) like
String str = "abcdbab";
System.out.println(str);
str = str.replace("ab", "A");
System.out.println(str);
Output is
abcdbab
AcdbA
Just change the following line:
char N = 65;
to
String N = "A";
and it'll work fine.
There is no such method String#replace(CharSequence, char), you will need to find the one that is closes to your needs and adjust to it, for example, there is a String#replaceAll(CharSequence, CharSequence) method and char can be represented as a CharSequence (or a String), for example...
S = S.replaceAll("ab", Character.toString(N));
You might like to have a read through Code Conventions for the Java TM Programming Language, it will make it easier for people to read your code and for you to read others
You can also change
S = S.replaceAll("ab", N);
to
S = S.replaceAll("ab", "" + N);
referencing here, http://www.tutorialspoint.com/java/java_string_replaceall.htm replaceAll takes a String, String not String, Char
I have a text which includes numerous unicode (?) characters in it, like the followings:
passaic$002c new jersey
Which should be : passaic, new jersey
Albert_W$002E_Barney
Which should be : albert w. barney
Roosevelt_High_School_$0028Yonkers$002C_New_York$0029
which should be: Roosevelt_High_School_(Yonkers,_New_York)
I searched the web and there is a big list of these characters: http://colemak.com/pub/mac/wordherd_source.txt
Do you know any fast method that I can replace these characters with their original characters? Note that I don't want to replace each of these characters one by one (like using replaceAll.) Instead I want to use a function that has already implemented this (maybe an external library)
Try native2ascii tool of java. Refer http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/native2ascii.html
Assuming those are UTF-16BE encoded values you can just use parse the values and cast to char:
public static String parse(CharSequence csq) {
StringBuilder out = new StringBuilder();
Matcher matcher = Pattern.compile("\\$(\\p{XDigit}{4}+)").matcher(csq);
int last = 0;
while (matcher.find()) {
out.append(csq.subSequence(last, matcher.start()));
String hex = matcher.group(1);
char ch = (char) Integer.parseInt(hex, 16);
out.append(ch);
last = matcher.end();
}
out.append(csq.subSequence(last, csq.length()));
return out.toString();
}
How to convert Strings to unicode? Characters are easy. But if I have "C" stored as a String, how can convert it to unicode? Because for characters, you just can use (int)charvariable but how to do for strings?
Actually I am using String.split() to split a String and then want to check if the 1st character is capital or small. Integer.parseInt is not working. It says NumberFormatException.
You may try this -
byte[] bytes = new byte[10];
String str = new String(bytes, Charset.forName("UTF-8"));
System.out.println(str);
for more detail you can see this tutorial
and for checking the first character you my use str.CharAt(0)