简体   繁体   中英

How to Store the String Value in Char array in java..?

I have to Store the String value in fixed char array, i tried but after storing the value char array size changed depends upon the String Value... Can Any one tell me how to fix that issue..

char[] uniqueID = new char[10];
String lUniqueID = mUniqueIdTxtFld.getText();  
uniqueID = lUniqueID.toCharArray();

O/P:

lUniqueID=12D;     
 it show in uniqueID[1,2,D]... 

But i need [1,2,D,,,,,,,,]

(ie) fixed Char array, it should not change.. Any one suggestion me what is wrong on that.

The current problem is that when doing uniqueID = lUniqueID.toCharArray(); you are assigning a new char array to uniqueID and not the content of it into the previous array defined.

What you want to achieve can be done using Arrays.copyOf .

char[] uniqueID = Arrays.copyOf(lUniqueID.toCharArray(), 10);

Use this method

System.arraycopy

and copy the chars from uniqueID = lUniqueID.toCharArray();
to some 10 chars long array arr which you created up-front.

You may try this:

public static void main(String arguments[]) {
    char[] padding = new char[10];
    char[] uniqueID = mUniqueIdTxtFld.getText().toCharArray();
    System.arraycopy(uniqueID, 0, padding, 0, Math.min(uniqueID.length, padding.length));
    System.out.println(Arrays.toString(padding));
}

Here is the snippet:

char[] uniqueID = new char[10];
String lUniqueID = mUniqueIdTxtFld.getText();
char[] compactUniqueID = lUniqueID.toCharArray();
System.arraycopy(compactUniqueID, 0, uniqueID, 0, compactUniqueID.length);

您是否尝试过yourString.getChars(params)方法?

    lUniqueId.getChars(0,uniqueID.length,uniqueID, 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM