I have this problem, I receive a String in a method that in database must be limited to 200(Varchar), with certain characters although the length of the String is less than 200, apparently the bytes length is more than 200, so I tried to make this:
Get the bytes length of the String
byte[] nameBytes = name.getBytes("UTF-8");
then if nameBytes.length > 200 I try to create a new String with a subarray of the original nameBytes like this:
name = new String(Arrays.copyOfRange(nameBytes, 0, 200), "UTF-8");
I am sure that Arrays.copyOfRange(nameBytes, 0, 200) is returning an array of length 200, but for some reason when I create the new String, this revision name.getBytes("UTF-8").length gives me 201, so I dont know why is adding one more byte.
There is something I am doing wrong? or there is a way to be sure o creating an array of the same length of the char array?
Thanks in advance.