[英]java copy string of characters between two special characters with split
String Code = "[123456]Ahmed"
String[] code_after_Cut = code.split("\\]" , "\\[");
我需要从代码字符串中剪切“ 123456”
您可以分两步尝试进行操作。 顺便说一下,你在代码中有一个大写的c
public static void main(String args[]) {
String code = "[123456]Ahmed";
System.out.println("code : " + code.split("\\]")[0].split("\\[")[1]);
//or with regex it can goes like this
Pattern patern = Pattern.compile("([0-9]{6})");
Matcher matcher = patern.matcher(code);
while (matcher.find()) {
System.out.println("code: " + matcher.group(1));
}
}
// as @RealSkeptic says... Regular expressions might be your point...
import java.until.regex.*;
....
String code = "[123456]Ahmed";
Pattern P = Pattern.compile("\\[(\\d+?\\)]Ahmed");
Matcher m = P.matcher(code);
String numberStr = null;
if (m.find()) numberStr = m.group(1);
// should print: 123456
我不认为它的名称总是“ Ahmed”,所以我建议这样做:
val full = "[123456]Achmed"
val pattern = Pattern.compile("\\[([0-9]*)\\].*")
val matcher = pattern.matcher(full)
if (matcher.matches())
Log.d("TAG", matcher.group(1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.