Daniele Milani
Daniele Milani

Reputation: 573

Creating string with escape java

I have the following problem, I have a string array like that

String[] myArray = {"AAAA","BBBB","CCCC"};

and my purpose is to create another array like that

String myNewArray = {"\uAAAA","\uBBBB","\uCCCC"};

The problem is that if I try to create the array using a cycle

for (int i=0; i<myArray.length; i++) {
    myNewArray[i] = "\u" + myArray[i];
}

I receive an "Invalid unicode error", if I use a cycle like that

for (int i=0; i<myArray.length; i++) {
    myNewArray[i] = "\\u" + myArray[i];
}

I obtain this array

String myNewArray = {"\\uAAAA","\\uBBBB","\\uCCCC"};

And if I use this cycle

for (int i=0; i<myArray.length; i++) {
    myNewArray[i] = "\\u" + myArray[i];
    myNewArray[i] = myNewArray[i].substring(1);
}

I obtain this array

String myNewArray = {"uAAAA","uBBBB","uCCCC"};

Does anyone know how I can do that?

Thanks

Upvotes: 6

Views: 2161

Answers (6)

Udanesh N
Udanesh N

Reputation: 171

You can just copy and paste the below program..i tested the code..and it is working fine

public class Main {

static String a[];
private static String[] myNewArray;

public static void main(String[] args) {


    String[] myArray = {"AAAA", "BBBB", "CCCC"};
    myNewArray = new String[myArray.length];
    for (int i = 0; i < myArray.length; i++) {
        myNewArray[i] = "\\u" + myArray[i];
    }
    for (int i = 0; i < myArray.length; i++) {
        System.out.println(myNewArray[i]);
    }
}

}

Upvotes: 0

user2504380
user2504380

Reputation: 545

I actually can't solve your prob, but i can tell you the following: Your first approach trys to concat a unicode string "\u" (in your case empty <=> invalid) with an nonunicode string. Your second approach is actually correct. System.out.println( "\u" + "AAAA" ) should print \uAAAA As a result one can say that your code is correct, my suggestion is to search for encoding options in your runtime enviroment / IDE.

Upvotes: 0

Joni
Joni

Reputation: 111269

You have to parse the strings as hexadecimal integers and then convert to chars:

String[] myArray = {"AAAA", "BBBB", "CCCC"};
String[] myNewArray = new String[myArray.length];
for (int i=0; i<myArray.length; i++) {
    char c = (char) Integer.parseInt(myArray[i], 16);
    myNewArray[i] = String.valueOf(c);
}

Upvotes: 7

Sanjaya Liyanage
Sanjaya Liyanage

Reputation: 4746

String[] myArray = {"AAAA","BBBB","CCCC"};
String[] myNewArray = {"\uAAAA","\uBBBB","\uCCCC"};
        String we="\\u";
        for (int i=0; i<myArray.length; i++) {
    myNewArray[i] = we + myArray[i];

}

Upvotes: -2

Jean Logeart
Jean Logeart

Reputation: 53829

The \ character needs to be escaped.

Therefore, doing myNewArray[i] = "\\u" + myArray[i] is actually what you want to do.

Try to print it to clear the matter up.

Upvotes: 0

Andreas Dolk
Andreas Dolk

Reputation: 114787

\uAAAA is a literal, not a String with five chars. So we can't create it with concatenation. It is one char.

Upvotes: 1

Related Questions