[英]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.. 我必须将String值存储在固定的char数组中,我尝试过,但是在存储值后,char数组的大小更改取决于String值。可以有人告诉我如何解决该问题。
char[] uniqueID = new char[10];
String lUniqueID = mUniqueIdTxtFld.getText();
uniqueID = lUniqueID.toCharArray();
O/P: O / P:
lUniqueID=12D;
it show in uniqueID[1,2,D]...
But i need [1,2,D,,,,,,,,]
但是我需要
[1,2,D,,,,,,,,]
(ie) fixed Char array, it should not change.. Any one suggestion me what is wrong on that. (即)固定的Char数组,它不应更改。任何人建议我这有什么问题。
The current problem is that when doing uniqueID = lUniqueID.toCharArray();
当前的问题是当执行
uniqueID = lUniqueID.toCharArray();
you are assigning a new char array to uniqueID
and not the content of it into the previous array defined. 您是将一个新的char数组分配给
uniqueID
而不是将其内容分配到先前定义的数组中。
What you want to achieve can be done using Arrays.copyOf
. 您想要实现的目标可以使用
Arrays.copyOf
完成。
char[] uniqueID = Arrays.copyOf(lUniqueID.toCharArray(), 10);
Use this method 使用这个方法
System.arraycopy System.arraycopy
and copy the chars from uniqueID = lUniqueID.toCharArray();
并从
uniqueID = lUniqueID.toCharArray();
复制字符uniqueID = lUniqueID.toCharArray();
to some 10 chars long array arr
which you created up-front. 到您
arr
创建的大约10个字符的长数组arr
。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.