String from an incomplete character array

The following code obviously gives a rather strange result.

char[] data = new char[5]; data[0] = 'a'; data[1] = 'b'; data[2] = 'c'; out.println("'" + new String(data) + "'"); 

'ab β–‘β–‘

Is there a way to create a string from an array of characters that takes into account that the entire array cannot be completely filled with characters?


Reason for the question: When using the Reader.read(char[]) method, you give it an array of characters to fill in, which I can only assume that it will not be completely filled if you are not lucky when you reach the end of the stream. I wonder how you could turn this into a string that you could add to a StringBuffer . Implement now, although the read method actually returns the number of bytes read, which I believe can be used in conjunction with StringBuffer.append(char[], int, int) , which makes my question moot. But, still, something that interests me, and not what I managed to find on googling, so I think this question is good to get an answer here;)

+3
source share
3 answers

String has a constructor of String (char [] value, int offset, int count) that takes a char array plus length (and offset):

 String s = new String(data, 0, 3); 

Assuming no embedded null characters (where the leading null character is considered an embedded null) in data , the solution should find the first null character to determine the number of char in data :

 int length = 0; while (length < data.length && 0 != data[length]) length++; String s = new String(data, 0, length); 
+1
source

I cannot think of a reliable method that does not work as follows:

 char[] data = new char[5]; data[0] = 'a'; data[1] = 'b'; data[2] = 'c'; StringBuilder builder = new StringBuilder(); for (char c : data) { if (c != 0x00) { builder.append(c); } } System.out.println(builder.toString()); 

This explicitly omits characters with a default value ( 0x00 ). Note that this is far from ideal, since 0x00 is a legal char. I would suggest you reevaluate design decisions that will lead you to this issue.

I'm not a fan of solutions suggesting that ((char) 0x00) is a space and can be truncated. Even if it works. Note:

 System.out.println(' ' == ((char) 0x00)); 

will print false .

+1
source

Yes, the constructor string with offset and length is used.

  String(char[] value, int offset, int count) new String(data,0,3) 

Now, if the exact value is unknown, you can write a utility method to fix the array. Or use trim() to trim the trailing characters.

  public static char[] getNotMessedUpArray(char[] messedUp) { int messedUpIndex = messedUp.length; for (int i = 0; i < messedUp.length; i++) { if (messedUp[i] <= ' ') { System.arraycopy(messedUp, i + 1, messedUp, i, messedUp.length - i - 1); } } for (int i = 0; i < messedUp.length; i++) { if (messedUp[i] <= ' ') { messedUpIndex--; } } char[] notMessedUp = new char[messedUpIndex]; System.arraycopy(messedUp, 0, notMessedUp, 0, messedUpIndex); return notMessedUp; } 
0
source

Source: https://habr.com/ru/post/1438170/


All Articles