Java - Write code to get String Length in byte

Requirements

Write code to get String Length in byte

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String str = "book2s.com";
        System.out.println(getStringLength(str));
    }// www  .j  a va  2 s  .  c  o  m

    public static int getStringLength(String str) {
        int result = 0;
        if (str != null) {
            result = str.getBytes().length;
        }
        return result;
    }
}