使用`substring()`:
javascript
let text = Hello, World!;
let substring = text.substring(7, 12); // 提取World
console.log(substring); // 输出: World
使用正则表达式:
javascript
let text = Email: example@65.hk;
let match = text.match(/[\w\.]+@[\w\.]+/);
if (match) {
console.log(match[0]); // 输出: example@65.hk
}
Java
在Java中,可以使用`substring()`方法或正则表达式来提取字符串的一部分。
使用`substring()`:
java
public class Main {
public static void main(String[] args) {
String text = Hello, World!;
String substring = text.substring(7, 12); // 提取World
System.out.println(substring); // 输出: World
}
}