Java String Chomp chomp(String str)

Here you can find the source of chomp(String str)

Description

chomp

License

Apache License

Declaration

public static String chomp(String str) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static String chomp(String str) {
        if (isEmpty(str)) {
            return str;
        }// w w w . ja v a 2 s .  c o m
        if (str.length() == 1) {
            char ch = str.charAt(0);
            if ((ch == '\r') || (ch == '\n')) {
                return "";
            }
            return str;
        }
        int lastIdx = str.length() - 1;
        char last = str.charAt(lastIdx);
        if (last == '\n') {
            if (str.charAt(lastIdx - 1) == '\r') {
                lastIdx--;
            }
        } else if (last != '\r') {
            lastIdx++;
        }
        return str.substring(0, lastIdx);
    }

    public static String chomp(String str, String separator) {
        if ((isEmpty(str)) || (separator == null)) {
            return str;
        }
        if (str.endsWith(separator)) {
            return str.substring(0, str.length() - separator.length());
        }
        return str;
    }

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

    public static String substring(String str, int start) {
        if (str == null) {
            return null;
        }
        if (start < 0) {
            start = str.length() + start;
        }
        if (start < 0) {
            start = 0;
        }
        if (start > str.length()) {
            return "";
        }
        return str.substring(start);
    }

    public static String substring(String str, int start, int end) {
        if (str == null) {
            return null;
        }
        if (end < 0) {
            end = str.length() + end;
        }
        if (start < 0) {
            start = str.length() + start;
        }
        if (end > str.length()) {
            end = str.length();
        }
        if (start > end) {
            return "";
        }
        if (start < 0) {
            start = 0;
        }
        if (end < 0) {
            end = 0;
        }
        return str.substring(start, end);
    }
}

Related

  1. chomp(String path)
  2. chomp(String s)
  3. chomp(String s)
  4. chomp(String sd, String c)
  5. chomp(String str)
  6. chomp(String str)
  7. chomp(String str)
  8. chomp(String str)
  9. chomp(String str, String sep)