Java String Chomp chomp(StringBuilder sb)

Here you can find the source of chomp(StringBuilder sb)

Description

chomp

License

LGPL

Declaration

public static void chomp(StringBuilder sb) 

Method Source Code

//package com.java2s;
/*/*from   ww w.j a va2s  .c o m*/
 * JBoss, the OpenSource J2EE webOS
 * 
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 *
 * Portions of this file Copyright (C) 2013 Jeremy D Monin <jeremy@nand.net>
 */

public class Main {
    public static void chomp(StringBuilder sb) {
        chopIfMatch(sb, '\n');
        chopIfMatch(sb, '\r');
    }

    public static String chomp(String input) {
        StringBuilder sb = new StringBuilder(input);
        chomp(sb);
        return sb.toString();
    }

    private static void chopIfMatch(StringBuilder sb, char ch) {
        if (sb.length() != 0 && sb.charAt(sb.length() - 1) == ch)
            sb.setLength(sb.length() - 1);
    }
}

Related

  1. chomp(String str)
  2. chomp(String str)
  3. chomp(String str, String sep)
  4. chomp(String str, String separator)
  5. chomp(String value)
  6. chompAllAndTrim(String str)
  7. chompClosureOpen(String expr)
  8. chompHeader(String str, String prex)
  9. chompLast(String str)