Here you can find the source of chomp(String str)
public static String chomp(String str)
//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); } }