Java 字符串中的空字符
我正在尝试用 Java 构建一个字符串,该字符串的最大长度为 3,最小长度为 1。
我正在根据整数数组的内容构建字符串,并且如果数组的内容为 -1,则希望在字符串中输出空字符。否则,字符串将包含整数的字符版本。
for (int i=0; i < mTypeSelection.length; i++){
mMenuName[i] = (mTypeSelection[i] > -1 ? Character.forDigit(mTypeSelection[i], 10) : '\u0000');
}
这是我到目前为止所拥有的,但是当我输出数组 {0,-1,-1} 的字符串而不是仅仅获取字符串“0”时,我得到字符串“0��”。
有谁知道我怎样才能得到我想要的结果。
谢谢,我
I'm trying to build a string in Java which will be at maximum 3 long and at minimum 1 long.
I'm building the string depending on the contents of a integer array and want to output a null character in the string if the contents of the array is -1. Otherwise the string will contain a character version of the integer.
for (int i=0; i < mTypeSelection.length; i++){
mMenuName[i] = (mTypeSelection[i] > -1 ? Character.forDigit(mTypeSelection[i], 10) : '\u0000');
}
This what I have so far but when I output the string for array {0,-1,-1} rather than just getting the string "0" I'm getting string "0��".
does anyone know how I can get the result I want.
Thanks, m
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您想在第一个空字符处终止字符串,就像在 C 中那样。但是,在 Java 中,字符串中可以有空字符,因此它们不会终止字符串。我认为以下代码会产生您所追求的行为:
I'm going to assume you want to terminate the string at the first null character, as would happen in C. However, you can have null characters inside strings in Java, so they won't terminate the string. I think the following code will produce the behaviour you're after: