Java String Sub String substring(String s, int start, int stop)

Here you can find the source of substring(String s, int start, int stop)

Description

substring

License

Apache License

Parameter

Parameter Description
s a parameter
start a parameter
stop a parameter

Return

string

Declaration

public static final String substring(String s, int start, int stop) 

Method Source Code

//package com.java2s;
/**// www.j  a va 2 s.  c  o  m
 *  Copyright 2011 Marco Berri - marcoberri@gmail.com
 *
 *  Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
 *
 * 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 {
    /**
     *
     * @param s
     * @param start
     * @param stop
     * @return string
     */
    public static final String substring(String s, int start, int stop) {
        if (isEmpty(s)) {
            return s;
        }
        if (start < 0 || start >= s.length()) {
            return "";
        }
        if (stop > s.length() || stop < 0) {
            stop = s.length();
        }
        return s.substring(start, stop);
    }

    /**
     *
     * @param s
     * @param start
     * @return string
     */
    public static final String substring(String s, int start) {
        return substring(s, start, -1);
    }

    /**
     * Checks if a String is empty or null
     *
     *   @param s String to check
     * @return true if null or empty, false otherwise
     */
    public static boolean isEmpty(String s) {
        return (s == null || "".equals(s.trim()));
    }

    /**
     *
     * @param s
     * @param alt
     * @return
     */
    public static String isEmpty(String s, String alt) {
        if (isEmpty(s)) {
            return alt;
        }
        return s;
    }

    /**
     * Esegue una Trim su una Stringa
     * se s == null return null
     *
     *   @param s String to trim
     * @return La Stringa Trimmata
     */
    public static final String trim(String s) {
        if (s == null) {
            return null;
        }
        return s.trim();
    }
}

Related

  1. substring(String s, int beginIndex, int endIndex)
  2. substring(String s, int i, String substring, int direction)
  3. subString(String s, int length)
  4. substring(String s, int start)
  5. substring(String s, int start, int length)
  6. subString(String s, int startIndex, int count)
  7. substring(String self, Integer startIndex, Integer endIndex)
  8. substring(String source, int beginIndex, int endIndex)
  9. substring(String source, int length, String padding)