Java String Chop chop(String str)

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

Description

chop

License

Apache License

Declaration

public static String chop(String str) 

Method Source Code

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

public class Main {
    public static String chop(String str) {
        if (str == null) {
            return null;
        }/*www  .  ja  v  a2 s  .com*/
        int strLen = str.length();
        if (strLen < 2) {
            return "";
        }
        int lastIdx = strLen - 1;
        String ret = str.substring(0, lastIdx);
        char last = str.charAt(lastIdx);
        if ((last == '\n') && (ret.charAt(lastIdx - 1) == '\r')) {
            return ret.substring(0, lastIdx - 1);
        }
        return ret;
    }

    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. chop(String src, String delim)
  2. chop(String str)
  3. chop(String str)
  4. chop(String str)
  5. chop(String str)
  6. chop(String str)
  7. chop(String str)
  8. chop(String str, int maxLength)
  9. chop(String string, int length)