Java equivalent of Visual Basic MidB$
-
15-06-2021 - |
Question
Is there an equivalent function in Java for MidB$?
The function returns the specified number of bytes from a string where the definition is MidB$(String As String, Start As Long, [Length] ) As String
It functions a bit similar to String.substring() but the length is defined in bytes.
Thanks.
Solution
You can try this:
int bytes = str.getBytes().length;
But this will be dependent on the character encoding of your platform.
More on getBytes()
.
OTHER TIPS
private String MidB(String str, int start, int len) {
byte[] bytes = str.getBytes()
String ret = "";
for (;start < len + start; start++)
ret = ret + (char) bytes[start];
return ret;
}
See if that works. And start won't get changed because primitives are passed by value in Java.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow