阅读量:0
在许多编程语言中,substring
方法或函数用于从一个给定的字符串中提取一部分字符
- Python:
string = "Hello, World!" start_index = 0 end_index = 5 sub_string = string[start_index:end_index] print(sub_string) # 输出 "Hello"
- JavaScript:
let string = "Hello, World!"; let startIndex = 0; let endIndex = 5; let subString = string.substring(startIndex, endIndex); console.log(subString); // 输出 "Hello"
- Java:
public class Main { public static void main(String[] args) { String string = "Hello, World!"; int startIndex = 0; int endIndex = 5; String subString = string.substring(startIndex, endIndex); System.out.println(subString); // 输出 "Hello" } }
- C#:
using System; class Program { static void Main() { string str = "Hello, World!"; int startIndex = 0; int length = 5; string subStr = str.Substring(startIndex, length); Console.WriteLine(subStr); // 输出 "Hello" } }
注意:不同编程语言中 substring
方法的参数和行为可能略有不同。请根据所使用的编程语言查阅相应文档。