Java 如何选择散列后的最后一个值?

Java 如何选择散列后的最后一个值?,java,Java,我有一个字符串,如下所示 如何选择哈希后的最后一个值 Fountain#Apple#Big(7) should give Big(7) Fountain#Orange(8) should give Orange(8) 我可以使用StringTokienizer来实现这一点,但我只想看看是否有更好、更简单的方法来实现这一点???您可以使用String类的split()方法,如下所示: String apples = "Fountain#Apple#Big(7)"; String

我有一个字符串,如下所示

如何选择哈希后的最后一个值

Fountain#Apple#Big(7)   should give Big(7) 
Fountain#Orange(8)      should give Orange(8) 

我可以使用StringTokienizer来实现这一点,但我只想看看是否有更好、更简单的方法来实现这一点???

您可以使用
String
类的
split()
方法,如下所示:

String apples = "Fountain#Apple#Big(7)";
String[] sections = apples.split("#");
// sections[0] == "Fountain"
// sections[1] == "Apple"
// sections[2] == "Big(7)"
String lastSection = sections.length > 0 ? sections[sections.length - 1] : null;
System.out.println(lastSection); // Prints: "Big(7)"

您可以使用
String
类的
split()
方法,如下所示:

String apples = "Fountain#Apple#Big(7)";
String[] sections = apples.split("#");
// sections[0] == "Fountain"
// sections[1] == "Apple"
// sections[2] == "Big(7)"
String lastSection = sections.length > 0 ? sections[sections.length - 1] : null;
System.out.println(lastSection); // Prints: "Big(7)"

您可以使用
String
类的
split()
方法,如下所示:

String apples = "Fountain#Apple#Big(7)";
String[] sections = apples.split("#");
// sections[0] == "Fountain"
// sections[1] == "Apple"
// sections[2] == "Big(7)"
String lastSection = sections.length > 0 ? sections[sections.length - 1] : null;
System.out.println(lastSection); // Prints: "Big(7)"

您可以使用
String
类的
split()
方法,如下所示:

String apples = "Fountain#Apple#Big(7)";
String[] sections = apples.split("#");
// sections[0] == "Fountain"
// sections[1] == "Apple"
// sections[2] == "Big(7)"
String lastSection = sections.length > 0 ? sections[sections.length - 1] : null;
System.out.println(lastSection); // Prints: "Big(7)"
s.substring(s.lastIndexOf(“#”)+1)
应该做这项工作。

s.substring(s.lastIndexOf(“#”)+1)
应该做这项工作。

s.substring(s.lastIndexOf(“#”)+1)
应该做这项工作。

s.substring(s.lastIndexOf(“#应该做这项工作。

Pattern p = Pattern.compile("(?<=#)([^#]*)$");
Matcher matcher = p.matcher(s);
if (matcher.find()) {
    String afterHash = matcher.group(1);
}
Pattern p=Pattern.compile((?regex版本:

Pattern p = Pattern.compile("(?<=#)([^#]*)$");
Matcher matcher = p.matcher(s);
if (matcher.find()) {
    String afterHash = matcher.group(1);
}
Pattern p=Pattern.compile((?regex版本:

Pattern p = Pattern.compile("(?<=#)([^#]*)$");
Matcher matcher = p.matcher(s);
if (matcher.find()) {
    String afterHash = matcher.group(1);
}
Pattern p=Pattern.compile((?regex版本:

Pattern p = Pattern.compile("(?<=#)([^#]*)$");
Matcher matcher = p.matcher(s);
if (matcher.find()) {
    String afterHash = matcher.group(1);
}

Pattern p=Pattern.compile(“(?过度拆分和索引计算?我通常发现正则表达式解决方案更健壮,因为它们不会出现一个接一个的错误。如果我不必处理没有散列的情况,它实际上会更干净。是否要重写它?我个人认为是这样,但每个人都有自己的解决方案。只给出答案,而不是问题,我想如何重写它ng/子字符串方法和索引数学更能自我记录。但对于任何寻找正则表达式解决方案的人来说,就是这样。过度拆分和索引数学?我通常发现正则表达式解决方案更健壮,因为它们不会出现一个接一个的错误。如果我不必处理没有散列的情况,它实际上会更干净。C我个人认为是这样的,但每个人都有自己的想法。只给出答案而不是问题,我认为拆分/子字符串方法和索引数学更能自我记录。但对于任何寻找正则表达式解决方案的人来说,就是这样。过度拆分和做索引数学?我通常发现正则表达式解决方案更健壮,因为它们没有I don’我不会遭受一个接一个的错误。如果我不必处理没有散列的情况,它实际上会更干净。是否要重写它?我个人认为是这样,但每个人都有自己的。只给出答案而不是问题,我想拆分/子字符串方法和索引数学更能自我记录。但对于任何寻找正则表达式的人来说,都是如此lution,就是这样。过度拆分和做索引数学?我通常发现正则表达式解决方案更健壮,因为它们不会出现一个接一个的错误。如果我不必处理没有散列的情况,它实际上会更干净。是否要重写它?我个人认为是这样,但每个人都有自己的答案。只给出答案,而不是问题,IMHO拆分/子字符串方法和索引数学更加自我记录。但是对于任何寻找正则表达式解决方案的人来说,这就是它。@Kiran如果这是你的答案,请将你的问题标记为solved@Kiran如果这是您的答案,请将您的问题标记为solved@Kiran如果这是您的答案,请将您的问题标记为solved@Kiran如果这这是你的答案,请将你的问题标记为已解决