substring(int beginIndex): This method returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string. substring(int beginIndex, int endIndex): The substring begins at the specified beginIndex and e...
public class SubstringExampleWithBothIndex { public static void main(String[] args) { String name = "codeRolls.com"; String result = name.substring(4, 9); System.out.println(result); } } Output: Rolls Note: substring(int beginIndex, int endIndex) method will throw IndexOutOfBoundsExcep...
publicclassSubstringExample{publicstaticvoidmain(String[]args){Stringstr="Hello12345World";Stringnum=str.substring(str.length()-5);System.out.println(num);// 输出:12345}} 1. 2. 3. 4. 5. 6. 7. 上面的代码中,我们使用substring方法截取了字符串"Hello12345World"中的最后5个字符,并将结果保存在...
如果要截取的字符可能出现多次,我们可以使用循环结构来实现多次截取。下面是示例代码: publicclassMultiSubstringExample{publicstaticvoidmain(String[]args){Stringstr="Hello, World!";charch='o';intindex=str.indexOf(ch);while(index!=-1){Stringresult=str.substring(index+1);System.out.println("截取结果:...
The Javasubstring()method extracts a part of thestring(substring) and returns it. Example classMain{publicstaticvoidmain(String[] args){ String str1 ="java is fun";// extract substring from index 0 to 3 System.out.println(str1.substring(0,4)); ...
1. String.substring() API The String.substring() in Java returns a new String that is a substring of the given string that begins from startIndex to an optional endIndex. These indices determine the substring position within the original string. The substring() method takes two arguments: beg...
The following example shows the usage of java String() method. public class Example { public static void main(String[] args) { String str = "The quick brown fox jumps over the lazy dog."; // Get a substring of the above string starting from ...
substring(int start) where Returns The function returns String object. Example 1 – substring(int start) In this example, we will create a StringBuilder object and initialize with some string passed as argument. We will then get the sub-string from the StringBuilder using substring() method from...
publicclassStringValuePassingExample{publicstaticvoidmain(String[] args){Stringoriginal="Hello"; modifyString(original); System.out.println("Original string after method call: "+ original); }publicstaticvoidmodifyString(String str){ str = str +", World!"; ...
public String substring(int startIndex) This methodreturnspart of thestring starting from specified index till end of the string Example publicclassSubstringExample1{ publicstaticvoidmain(Stringargs[]){ Stringstr="javainsimpleway"; System.out.println(str.substring(6)); ...