strip Leading Data from a string - Java java.lang

Java examples for java.lang:String Strip

Description

strip Leading Data from a string

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String source = "java2s.com";
        String charsToStrip = "b";
        System.out.println(stripLeadingData(source, charsToStrip));
    }//from  w w w.  j a v  a  2s .co  m

    public static String stripLeadingData(String source, String charsToStrip) {
        if (isNotEmpty(source) && isNotEmpty(charsToStrip)) {
            StringBuilder sb = new StringBuilder(source);
            while (sb.length() > 0
                    && charsToStrip.indexOf(sb.charAt(0)) >= 0) {
                sb.deleteCharAt(0);
            }
            source = sb.toString();
        }
        return source;
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static int length(String source) {
        int result = 0;
        if (isNotEmpty(source)) {
            result = source.length();
        }
        return result;
    }

    public static boolean isEmpty(String str) {
        return (str == null || str.length() == 0);
    }
}

Related Tutorials