Search code examples
javavbacode-conversion

Java equivalent of Visual Basic MidB$


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().