0

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.

nmenego
  • 846
  • 3
  • 17
  • 36
  • In Java a String is composed of characters, not bytes. Do you really want to get the number of *bytes* or do you actually want the number of *characters*? –  Jun 19 '12 at 06:56
  • Thanks for pointing that out. Yes, I actually want to retrieve the number of characters given the specified number of bytes. – nmenego Jun 19 '12 at 07:12

2 Answers2

1

You can try this:

int bytes = str.getBytes().length;

But this will be dependent on the character encoding of your platform.

More on getBytes().

Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
0
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.

iracigt
  • 282
  • 1
  • 5