1. substrings
You can extract a substring using substring( ). It has two forms. The first is String substring(int startIndex)
Here, startIndex specifies the index at which the substring will begin. This form returns a copy of the substring that begins at startIndex and runs to the end of the invoking string.
The second form of substring( ) allows you to specify both the beginning and ending index of the substring:
String substring(int startIndex, int endIndex) Here, startIndex specifies the beginning index, and endIndex specifies the stopping point. The string returned contains all the characters from the beginning index, up to, but not including, the ending index. The following program uses substring( ) to replace all instances of one substring with another within a string:
public class SubstringExample1{
public static void main(String[] args){
String string = "Rajesh kumar";
System.out.println("String : " + string);
String substring = string.substring(3);
System.out.println("String after 3rd index:
" + substring);
substring = string.substring(1, 2);
System.out.println("Substring (1,2): " +
substring);
}
}
program result
String : Rajesh kumar String after 3rd index: esh kumar Substring (1,2): a
2. tutorial length() charAt()
import java.io.*;
class CharAt{
public static void main(String[] args){
try{
String s="roseindia";
int len=s.length();
System.out.println(len);
char char1=s.charAt(4);
System.out.println(char1);
}
catch(Exception e){}
}
}
Output this program:
roseindia
9
i
You can extract a substring using substring( ). It has two forms. The first is String substring(int startIndex)
Here, startIndex specifies the index at which the substring will begin. This form returns a copy of the substring that begins at startIndex and runs to the end of the invoking string.
The second form of substring( ) allows you to specify both the beginning and ending index of the substring:
String substring(int startIndex, int endIndex) Here, startIndex specifies the beginning index, and endIndex specifies the stopping point. The string returned contains all the characters from the beginning index, up to, but not including, the ending index. The following program uses substring( ) to replace all instances of one substring with another within a string:
public class SubstringExample1{
public static void main(String[] args){
String string = "Rajesh kumar";
System.out.println("String : " + string);
String substring = string.substring(3);
System.out.println("String after 3rd index:
" + substring);
substring = string.substring(1, 2);
System.out.println("Substring (1,2): " +
substring);
}
}
program result
String : Rajesh kumar String after 3rd index: esh kumar Substring (1,2): a
2. tutorial length() charAt()
import java.io.*;
class CharAt{
public static void main(String[] args){
try{
String s="roseindia";
int len=s.length();
System.out.println(len);
char char1=s.charAt(4);
System.out.println(char1);
}
catch(Exception e){}
}
}
Output this program:
roseindia
9
i