Java String Sub String substringBeforeChar(String str, int separator)

Here you can find the source of substringBeforeChar(String str, int separator)

Description

Return the substring before the first occurrence of a separator.

License

Open Source License

Parameter

Parameter Description
str the string to get a substring from, may be null
separator the character to search for

Return

the substring before the first occurrence of the separator

Declaration

public static String substringBeforeChar(String str, int separator) 

Method Source Code

//package com.java2s;
/*//from w  ww. j a v a  2  s.co m
 * Copyright (c) 2012, the Dart project authors.
 * 
 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

public class Main {
    /**
     * Return the substring before the first occurrence of a separator. The separator is not included
     * in the returned value.
     * <p>
     * A {@code null} string input will return {@code null}. An empty ("") string input will return
     * the empty string.
     * <p>
     * If nothing is found, the string input is returned.
     * 
     * <pre>
     * StringUtils.substringBefore(null, *)      = null
     * StringUtils.substringBefore("", *)        = ""
     * StringUtils.substringBefore("abc", 'a')   = ""
     * StringUtils.substringBefore("abcba", 'b') = "a"
     * StringUtils.substringBefore("abc", 'c')   = "ab"
     * StringUtils.substringBefore("abc", 'd')   = "abc"
     * </pre>
     * 
     * @param str the string to get a substring from, may be null
     * @param separator the character to search for
     * @return the substring before the first occurrence of the separator
     */
    public static String substringBeforeChar(String str, int separator) {
        if (isEmpty(str)) {
            return str;
        }
        int pos = str.indexOf(separator);
        if (pos < 0) {
            return str;
        }
        return str.substring(0, pos);
    }

    /**
     * Return {@code true} if the given CharSequence is empty ("") or null.
     * 
     * <pre>
     * StringUtils.isEmpty(null)      = true
     * StringUtils.isEmpty("")        = true
     * StringUtils.isEmpty(" ")       = false
     * StringUtils.isEmpty("bob")     = false
     * StringUtils.isEmpty("  bob  ") = false
     * </pre>
     * 
     * @param cs the CharSequence to check, may be null
     * @return {@code true} if the CharSequence is empty or null
     */
    public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }
}

Related

  1. substringBefore(String str, String separator)
  2. substringBefore(String str, String separator)
  3. substringBefore(String str, String separator)
  4. substringBefore(String string, String delimiter)
  5. substringBefore(String text, String str)
  6. substringBeforeFirst(String string, String delimiter)
  7. subStringBeforeFirstTab(final String s)
  8. substringBeforeLast(final String str, final String separator)
  9. substringBeforeLast(String str, String separator)