Android String Sub String Get substringBefore(String text, char separator)

Here you can find the source of substringBefore(String text, char separator)

Description

Gives the substring of the given text before the given separator.

Parameter

Parameter Description
text a parameter
separator a parameter

Declaration

public static String substringBefore(String text, char separator) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from w w  w  .jav a  2 s  .com*/
     * Gives the substring of the given text before the given separator.
     * 
     * If the text does not contain the given separator then the given text is returned.
     * 
     * @param text
     * @param separator
     * @return
     */
    public static String substringBefore(String text, char separator) {
        if (isEmpty(text)) {
            return text;
        }
        int sepPos = text.indexOf(separator);
        if (sepPos < 0) {
            return text;
        }
        return text.substring(0, sepPos);
    }

    /**
     * Whether the given string is null or zero-length.
     * 
     * @param text
     * @return
     */
    public static boolean isEmpty(String text) {
        return (text == null) || (text.length() == 0);
    }
}

Related

  1. takeOutFirstChar(String input)
  2. substring(String str, int srcPos, int specialCharsLength)
  3. substringAfter(String text, char c)
  4. substringAfterLast(String text, char separator)
  5. substringBefore(String text, char separator)
  6. substringBeforeLast(String text, char separator)