Java String Index Of sub(String string, int fromIndex, int toIndex)

Here you can find the source of sub(String string, int fromIndex, int toIndex)

Description

sub

License

Open Source License

Declaration

public static String sub(String string, int fromIndex, int toIndex) 

Method Source Code

//package com.java2s;

import java.util.Arrays;

public class Main {
    public static final String EMPTY = "";

    public static String sub(String string, int fromIndex, int toIndex) {
        int len = string.length();

        if (fromIndex < 0) {
            fromIndex = len + fromIndex;

            if (toIndex == 0) {
                toIndex = len;/*from ww w.  ja  v  a 2  s.com*/
            }
        }

        if (toIndex < 0) {
            toIndex = len + toIndex;
        }

        if (toIndex < fromIndex) {
            int tmp = fromIndex;
            fromIndex = toIndex;
            toIndex = tmp;
        }

        if (fromIndex == toIndex) {
            return EMPTY;
        }

        char[] strArray = string.toCharArray();
        char[] newStrArray = Arrays.copyOfRange(strArray, fromIndex, toIndex);
        return new String(newStrArray);
    }
}

Related

  1. lastIndexOfIgnoreCase(final String s, final String subS)
  2. mergeStringLines(String lineOne, String lineTwo, int keyIndex, int insertingIndex)
  3. readColumn(int columnIndex, String inputString, String columnSeparator)
  4. removeColumn(String line, int index)
  5. splitUsingIndexOf(String splittee, String splitter)
  6. subStringChinese(String str, int startIndex, int endIndex)