Java String Sub String subStringToInteger(String src, String start, String to)

Here you can find the source of subStringToInteger(String src, String start, String to)

Description

Example: subString("12345","1","4")=23

License

Apache License

Parameter

Parameter Description
src a parameter
start a parameter
to a parameter

Declaration

public static Integer subStringToInteger(String src, String start, String to) 

Method Source Code

//package com.java2s;
/*//from   w ww .j ava2 s .  c o  m
 * Copyright 1999-2101 Alibaba Group Holding Ltd.
 *
 * 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 {
    /**
     * Example: subString("12345","1","4")=23
     * 
     * @param src
     * @param start
     * @param to
     * @return
     */
    public static Integer subStringToInteger(String src, String start, String to) {
        return stringToInteger(subString(src, start, to));
    }

    /**
     * @param in
     * @return
     */
    public static Integer stringToInteger(String in) {
        if (in == null) {
            return null;
        }
        in = in.trim();
        if (in.length() == 0) {
            return null;
        }

        try {
            return Integer.parseInt(in);
        } catch (NumberFormatException e) {
            return null;
        }
    }

    /**
     * Example: subString("abcd","a","c")="b"
     * 
     * @param src
     * @param start null while start from index=0
     * @param to null while to index=src.length
     * @return
     */
    public static String subString(String src, String start, String to) {
        int indexFrom = start == null ? 0 : src.indexOf(start);
        int indexTo = to == null ? src.length() : src.indexOf(to);
        if (indexFrom < 0 || indexTo < 0 || indexFrom > indexTo) {
            return null;
        }

        if (null != start) {
            indexFrom += start.length();
        }

        return src.substring(indexFrom, indexTo);

    }
}

Related

  1. subStringNotEncode(String subject, int size)
  2. subStringRight(String str, int length)
  3. substrings(String str, int start, int end)
  4. subStrings(String str1, String str2)
  5. substrings(String[] arr, int start, int end)
  6. substringToLast(final String str, final String separator)
  7. substringUntil(String org, int begin, char term)
  8. substringUntilMatch(String string, String match)
  9. subStringWhiteSpaces(String str, int maxLen)