Java String Sub String substringBefore(String str, int endChar)

Here you can find the source of substringBefore(String str, int endChar)

Description

substring Before

License

MIT License

Declaration

public static String substringBefore(String str, int endChar) 

Method Source Code

//package com.java2s;
/*!/* www  .  j  a  va 2  s  . c  o m*/
 * mifmi-commons4j
 * https://github.com/mifmi/mifmi-commons4j
 *
 * Copyright (c) 2015 mifmi.org and other contributors
 * Released under the MIT license
 * https://opensource.org/licenses/MIT
 */

public class Main {
    public static String substringBefore(String str, int endChar) {
        if (str == null) {
            return null;
        }
        if (endChar < 0) {
            return str;
        }

        int idx = str.indexOf(endChar);
        if (idx == -1) {
            return str;
        }

        return str.substring(0, idx);
    }

    public static String substringBefore(String str, String endStr) {
        if (str == null) {
            return null;
        }
        if (endStr == null) {
            return str;
        }

        int idx = str.indexOf(endStr);
        if (idx == -1) {
            return str;
        }

        return str.substring(0, idx);
    }

    public static int indexOf(CharSequence charSeq, char ch) {
        return indexOf(charSeq, ch, 0);
    }

    public static int indexOf(CharSequence charSeq, char ch, int fromIndex) {
        if (charSeq == null) {
            return -1;
        }
        int len = charSeq.length();
        for (int i = fromIndex; i < len; i++) {
            char c = charSeq.charAt(i);
            if (c == ch) {
                return i;
            }
        }
        return -1;
    }

    public static int indexOf(CharSequence charSeq, char[] ch) {
        return indexOf(charSeq, ch, 0);
    }

    public static int indexOf(CharSequence charSeq, char[] ch, int fromIndex) {
        if (charSeq == null) {
            return -1;
        }
        int len = charSeq.length();
        for (int i = fromIndex; i < len; i++) {
            char c = charSeq.charAt(i);
            for (int j = 0; j < ch.length; j++) {
                if (c == ch[j]) {
                    return i;
                }
            }
        }
        return -1;
    }
}

Related

  1. substringBaseName(String baseName, int extraCharsNumber)
  2. substringBefore(final String str, final String separator)
  3. substringBefore(final String string, final String separator)
  4. substringBefore(String input, String sub)
  5. substringBefore(String s, char c)
  6. substringBefore(String str, String separator)
  7. substringBefore(String str, String separator)
  8. substringBefore(String str, String separator)
  9. substringBefore(String string, String delimiter)