Java String Sub String substringGuarded(String s, int position, int count)

Here you can find the source of substringGuarded(String s, int position, int count)

Description

Like String#substring(int) but allows for the count parameter to extend past the string's bounds.

License

Open Source License

Declaration

public static String substringGuarded(String s, int position, int count) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

public class Main {
    /**//from   w  ww  .  ja  v a 2 s  . c  om
     * Like {@link String#substring(int)} but allows for the {@code count}
     * parameter to extend past the string's bounds.
     */
    public static String substringGuarded(String s, int position, int count) {
        int sLength = s.length();
        if (sLength - position <= count) {
            return position == 0 ? s : s.substring(position);
        } else {
            return s.substring(position, position + count);
        }
    }
}

Related

  1. substringEnd(String string, int start, int length)
  2. substringEquals(final String s1, final int fromIndex1, final int toIndex1, final String s2, final int fromIndex2, final int toIndex2)
  3. subStringExe(String s, String jmp, String sb, String se)
  4. substringFromMatch(String match, String string)
  5. substringFromMultiValuedFields(int start, int end, String[] fieldValues, int offsetGap, String interFieldJoiner)
  6. subStringIgnoreCase(String str, String separator, Integer stratNum, Integer endNum)
  7. substringInBetween(String name, String prefix, String delimiter)
  8. substringL(String s, int i, int len)
  9. substringLinesWithTokenOfEOL( String originalString, String stringToBeInserted)