String类的substring(int beginIndex,int endIndex)方法。它返回一个新字符串,该字符串是该字符串的子字符串。子字符串从指定的beginIndex开始,并扩展到索引endIndex-1处的字符。因此,子字符串的长度为endIndex-beginIndex。
import java.lang.*; public class StringDemo { public static void main(String[] args) { String str = "This is nhooo.com"; String substr = ""; // prints the substring after index 8 till index 17 substr = str.substring(8, 17); System.out.println("substring = " + substr); //在索引0到索引8之后打印子字符串 substr = str.substring(0, 8); System.out.println("substring = " + substr); } }
输出结果
substring = tutorials substring = This is