cut String to length - Java java.lang

Java examples for java.lang:String Truncate

Introduction

The following code shows how to cut String.

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String source = "java2s.com";
        int slength = 42;
        System.out.println(cutString(source, slength));
    }/*from   w w  w. j ava 2 s.co  m*/

    public static String cutString(String source, String output, int slength) {
        String returnVal = null;
        if (source != null) {
            if (source.length() > slength) {
                returnVal = source.substring(0, slength) + output;
            } else
                returnVal = source;
        }
        return returnVal;
    }

    public static String cutString(String source, int slength) {
        String result = null;
        if (source != null) {
            if (source.length() > slength) {
                result = source.substring(0, slength);
            } else
                result = source;
        }
        return result;
    }
}

Related Tutorials