Do substring and handle null value - Java java.lang

Java examples for java.lang:String Substring

Description

Do substring and handle null value

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String src = "java2s.com";
        int beginIndex = 2;
        int endIndex = 4;
        System.out.println(substring(src, beginIndex, endIndex));
    }//ww w.  ja  va  2 s.  c o  m

    public static String substring(String src, int beginIndex, int endIndex) {
        if (isNull(src))
            return null;
        if (beginIndex < 0)
            return src.substring(0, endIndex);

        if (endIndex > src.length())
            return src.substring(beginIndex, src.length());

        return src.substring(beginIndex, endIndex);
    }

    public static boolean isNull(String src) {
        if (src == null)
            return true;
        else
            return false;
    }
}

Related Tutorials