Java String Truncate truncateAt(String str, char chr)

Here you can find the source of truncateAt(String str, char chr)

Description

Trim the end off of a string starting at the specified character.

License

Open Source License

Parameter

Parameter Description
str String to trim
chr char to trim at

Return

str turncated at the first occurrence of char, or the original string if no occurrence

Declaration

public static String truncateAt(String str, char chr) 

Method Source Code

//package com.java2s;
/*/*from  w  w  w .  jav a2s .  com*/
    
Copyright (c) 2000-2014 Board of Trustees of Leland Stanford Jr. University,
all rights reserved.
    
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
    
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
    
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
STANFORD UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    
Except as contained in this notice, the name of Stanford University shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from Stanford University.
    
*/

public class Main {
    /**
     * Trim the end off of a string starting at the specified character.
     *
     * @param str String to trim
     * @param chr char to trim at
     * @return str turncated at the first occurrence of char, or
     * the original string if no occurrence
     */
    public static String truncateAt(String str, char chr) {
        if (str == null) {
            return null;
        }
        int pos = str.indexOf(chr);
        if (pos < 0) {
            return str;
        }
        return str.substring(0, pos);
    }
}

Related

  1. truncate(String value, int maxLen, String ellipses)
  2. truncate(String x, int length)
  3. truncateAndTrailOffText(String text, int limit)
  4. truncateAndTrim0(String str, String delim)
  5. truncateAt(final String input, final String... substrings)
  6. truncateAtMaxLength(String source, int maxLength, boolean addEllipsis)
  7. truncateAtWhitespace(String text, int length)
  8. truncateAtWord(String input, int length)
  9. truncateBodyText(String input)