示例索引(场景1:Java的String类)
- 示例:获取应用的包名(去除前面N个字符,截取后面部分)
- 示例:获取应用包名(去除后面N个字符,截取前面部分)
- 示例:获取应用MD5(去除字符串中空格)
- 示例:获取AppOps的log中的timestamp(字符串分割)
- 示例:获取Windows路径中某个目录名字(字符串分割:)
- 示例:lsof获取联网应用的PID(字符串分割:无规律空格)
- 示例:AppOps的log,判断是包名还是时间戳(判断字符串是否包含数字)
示例:获取应用的包名(去除前面N个字符,截取后面部分)
public class Test {
public static void main(String[] args) {
String s = "package:com.android.cts.priv.ctsshim";
System.out.println(s.substring(8));
}
}
示例:获取应用包名(去除后面N个字符,截取前面部分)
public class Test {
public static void main(String[] args) {
String s = "com.android.cts.priv.ctsshim.apk";
System.out.println(s.substring(0, s.length() - 4));
}
}
示例:获取应用MD5(去除字符串中空格)
public class Test {
public static void main(String[] args) {
String md5 = " 0e 3b bd 10 30 a9 45 bb 42 f1 b5 71 48 b3 bc 07 ";
System.out.println(md5.replace(" ", ""));
}
}
示例:获取AppOps的log中的timestamp(字符串分割)
public class Test {
public static void main(String[] args) {
String s = "Access: [top-s] 2022-05-28 11:10:02.657 (-53m8s469ms) duration=+709ms";
System.out.println(s.split("\(")[0].split("]")[1]);
}
}
示例:获取Windows路径中某个目录名字(字符串分割:)
public class Test {
public static void main(String[] args) {
String s = "D:\project\PhoneSecScanner\tmp\class\android.ext.services\com\google\common\collect\MapMakerInternalMap.class";
System.out.println(s.split("\\")[5]);
}
}
参考资料:
java基础:字符串分隔反斜杠_椰果子的博客-CSDN博客_java split 反斜杠
示例:lsof获取联网应用的PID(字符串分割:无规律空格)
adb shell lsof | findstr IPv
C:>adb shell lsof | findstr IPv ims_rtp_daemon 3504 radio 19u IPv6 0t0 205132 UDP []:38088->[]:0 ims_rtp_daemon 3504 radio 20u IPv6 0t0 205136 UDP []:45658->[]:0 droid.bluetooth 4216 bluetooth 131u IPv4 0t0 665124 TCP :8872->:0 (LISTEN)
思路1:lsof提取PID(字符串分割)
- 为啥s* 匹配结果一个一个字符
public class Test {
public static void main(String[] args) {
String s = "ims_rtp_daemon 3504 radio 19u IPv6 0t0 205132 UDP []:38088->[]:0";
String[] s1 = s.split("\s+");
String[] s2 = s.split("\s*");
for (String s3 : s1) {
System.out.println(s3);
}
for (String s3 : s2) {
System.out.println(s3);
}
}
}
参考资料:
JAVA正则表达式匹配多个空格_予亭的博客-CSDN博客_正则匹配空格
思路2:lsof 提取 PID(字符串替换)
public class Test {
public static void main(String[] args) {
String s1 = "ims_rtp_daemon 3504 radio 19u IPv6 0t0 205132 UDP []:38088->[]:0";
while (s1.contains(" ")) {
s1 = s1.replace(" ", " ");
}
System.out.println(s1);
}
}
示例:AppOps的log,判断是包名还是时间戳(判断字符串是否包含数字)
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String s1 = "Package com.android.systemui:";
String s2 = "Access: [pers-s] 2022-05-09 18:56:27.117 (-49d4h33m48s601ms)";
System.out.println(Pattern.compile("\d").matcher(s1).find());
System.out.println(Pattern.compile("\d").matcher(s2).find());
}
}



