开发者

String to Char Array and Char array to String Conversion

开发者 https://www.devze.com 2023-04-01 21:28 出处:网络
In my program I am getting a string from Database result set and convert it to char array like this: emp.nid = rs.getString(\"nid\").toCharArray();

In my program I am getting a string from Database result set and convert it to char array like this:

emp.nid = rs.getString("nid").toCharArray();

In this part there is no error. The String is successfully converted to char array. B开发者_运维百科ut I have another code like this:

nid_txt.setText(emp.nid.toString());

This prints some freaky text. Not the original. Why was this happens? Please help me.


You're calling toString on a char[] - and that inherits the implementation from Object, so you get the char[].class name, @ and then the hash of the object. Instead, call the String(char[]) constructor:

nid_txt.setText(new String(emp.nid));


This happens because the toString() method is the String representation of the object, and not the String of what it contains.

Try doing like this:

nid_txt.setText(new String(emp.nid));


instead of foo.toString() do new String(foo).


You are calling the toString() on the array object. Try:

new String(emp.nid);

and you should see better results.


assuming that emp.nid is byte array second sentence is completely wrong. toString() method in such object won't work. Try insted creating new String based on byte array:

String s = new String(emp.nid);
nid_txt.setText(s);
0

精彩评论

暂无评论...
验证码 换一张
取 消