Java substring() 方法java
substring() 方法返回字符串的子字符串。python
语法数组
public String substring(int beginIndex)ide
或code
public String substring(int beginIndex, int endIndex)索引
参数字符串
beginIndex -- 起始索引(包括), 索引从 0 开始。string
endIndex -- 结束索引(不包括)。it
返回值class
子字符串。
实例
public class Test {
public static void main(String args[]) { String Str = new String("www.runoob.com"); System.out.print("返回值 :" ); System.out.println(Str.substring(4) ); System.out.print("返回值 :" ); System.out.println(Str.substring(4, 10) ); }
}
以上程序执行结果为:
返回值 :runoob.com
返回值 :runoob
Python中相似sunbstring应用方法
python中没有substring的定义,可是有更轻巧的实现,能够经过数组的slice来截取字符串
例如,在java中咱们这样截取字符串:
String s = "Hello OutOfMemory.CN";
String small = s.subString(2,4);
而在python中,咱们这样实现:
s = "Hello OutOfMemory.CN"
small = s[2:4]
python的用法更简单了。