Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    public static String trimStrInBytes(String strings, int Dstbytes) {
        byte[] origByte = strings.getBytes();
        if (origByte.length <= Dstbytes)
            return strings;

        System.out.println("old string" + strings + " len:" + strings.length() + " dstByte:" + Dstbytes);
        final int len = Dstbytes;
        String oldString = new String(origByte, 0, len);
        System.out.println("before trime string:" + oldString + " len:" + oldString.length());
        int oldLen = oldString.length();
        int newLen = 0;

        for (int i = 1; i <= len; i++) {
            String newString = new String(origByte, 0, len - i);
            System.out.println("new string:" + newString + " len:" + newString.length());
            newLen = newString.length();
            System.out.println("old:" + oldLen + " new:" + newLen);
            if (oldLen > newLen) {
                String trimmed = new String(origByte, 0, len - i);
                String sub = strings.substring(0, trimmed.length() + 1);
                if (sub.getBytes().length <= len)
                    return sub;
                //                        System.out.println("trimmed string:"+new String(origByte, 0, len-i));
                return trimmed;
            }
        }
        return "";
    }
}