Java String Chomp chomp(String inStr)

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

Description

Deletes the Line Feed (10) and Carriage Return (13) fields of the given String.

License

Open Source License

Parameter

Parameter Description
inStr The String to work on.

Return

The string without Line Feed and Carriage Return.

Declaration

public static String chomp(String inStr) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009 MATERNA Information & Communications. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html. For further
 * project-related information visit http://www.ws4d.org. The most recent
 * version of the JMEDS framework can be obtained from
 * http://sourceforge.net/projects/ws4d-javame.
 ******************************************************************************/

public class Main {
    /**/* ww w  . java 2  s  . c  o  m*/
     * Deletes the Line Feed (10) and Carriage Return (13) fields of the given
     * String.
     * 
     * @param inStr The String to work on.
     * @return The string without Line Feed and Carriage Return.
     */
    public static String chomp(String inStr) {
        if (inStr == null || "".equals(inStr)) {
            return inStr;
        }

        char lastChar = inStr.charAt(inStr.length() - 1);

        if (lastChar == 13) {
            // if the string ends with Carriage Return
            return inStr.substring(0, inStr.length() - 1);
        } else if (lastChar == 10) {
            // if the string ends with Line Feed
            String tmp = inStr.substring(0, inStr.length() - 1);
            if ("".equals(tmp)) {
                return tmp;
            }
            lastChar = tmp.charAt(tmp.length() - 1);
            // if the string also contains Carriage Return
            if (lastChar == 13) {
                return tmp.substring(0, tmp.length() - 1);
            } else {
                return tmp;
            }
        } else {
            return inStr;
        }
    }
}

Related

  1. chomp(CharSequence prefix, CharSequence full)
  2. chomp(final String s, final String suffix)
  3. chomp(String in)
  4. chomp(String options)
  5. chomp(String path)
  6. chomp(String s)
  7. chomp(String s)