strip Trailing Data from a string - Java java.lang

Java examples for java.lang:String Strip

Description

strip Trailing Data from a string

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String source = "java2s.com";
        String charsToStrip = "java2s.com";
        System.out.println(stripTrailingData(source, charsToStrip));
    }// ww  w.ja va2s . c  om

    public static String stripTrailingData(String source,
            String charsToStrip) {
        if (isNotEmpty(source) && isNotEmpty(charsToStrip)) {
            StringBuilder sb = new StringBuilder(source);
            while (sb.length() > 0
                    && charsToStrip.indexOf(sb.charAt(sb.length() - 1)) >= 0) {
                sb.deleteCharAt(sb.length() - 1);
            }
            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