Here you can find the source of substring(String s, int start, int length)
Parameter | Description |
---|---|
s | the original String |
start | starting position for the substring within the original string. 0-based index position |
length | length in characters of the substracted substring |
static String substring(String s, int start, int length)
//package com.java2s; /*//from w ww . j av a2 s . c o m * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ public class Main { /** * Extract a substring from the given string, using start index and length of the extracted substring. * * @param s the original String * @param start starting position for the substring within the original string. 0-based index position * @param length length in characters of the substracted substring * @return the resulting String */ static String substring(String s, int start, int length) { if (!hasLength(s)) { return s; } if (start < 0) start = 0; if (start + 1 > s.length() || length < 0) return ""; return (start + length > s.length()) ? s.substring(start) : s.substring(start, start + length); } private static boolean hasLength(String s) { return (s != null && s.length() > 0); } }