如何在java中从字符串中检索特定值

如何在java中从字符串中检索特定值,java,string,Java,String,我有一个字符串字段,其中包含值: String a = "Local/5028@from-queue-bd7f,1"; 现在,根据我的需要,我需要从上述字符串字段中提取值“5028”。如果您知道您的格式是一致的,则可以使用子字符串方法或使用/和@拆分字符串,然后从令牌数组中获取第二个值。如果您知道您的格式是一致的,则可以使用子字符串方法或使用/和@拆分字符串,然后从令牌数组中获取第二个值。使用字符串#子字符串函数检索值。您需要将开始索引和结束索引作为参数传递 String a = "Loca

我有一个字符串字段,其中包含值:

 String a = "Local/5028@from-queue-bd7f,1";

现在,根据我的需要,我需要从上述字符串字段中提取值“5028”。

如果您知道您的格式是一致的,则可以使用子字符串方法或使用/和@拆分字符串,然后从令牌数组中获取第二个值。

如果您知道您的格式是一致的,则可以使用子字符串方法或使用/和@拆分字符串,然后从令牌数组中获取第二个值。

使用
字符串#子字符串
函数检索值。您需要将开始索引和结束索引作为参数传递

String a = "Local/5028@from-queue-bd7f,1";
System.out.println(a.substring(a.indexOf('/')+1, a.indexOf('@')));
使用
String#substring
函数检索值。您需要将开始索引和结束索引作为参数传递

String a = "Local/5028@from-queue-bd7f,1";
System.out.println(a.substring(a.indexOf('/')+1, a.indexOf('@')));

如果此字符串始终为给定格式,则可以尝试以下操作:

String temp=a.split("@")[0];
System.out.println(temp.substring(temp.length()-4,temp.length()));

如果此字符串始终为给定格式,则可以尝试以下操作:

String temp=a.split("@")[0];
System.out.println(temp.substring(temp.length()-4,temp.length()));

这将拆分每个
/
@
上的字符串

String a = "Local/5028@from-queue-bd7f,1";
System.out.println(a.split("[/@]")[1]);

这将拆分每个
/
@
上的字符串

String a = "Local/5028@from-queue-bd7f,1";
System.out.println(a.split("[/@]")[1]);

使用正则表达式:

 String a = "Local/5028@from-queue-bd7f,1";
         Pattern p = Pattern.compile("\\d+");
         Matcher m = p.matcher(a);
         System.out.println(m.find() + " " + m.group());
String a = "Local/5028@from-queue-bd7f,1";
         String[] split = a.split("/");
         System.out.println(split[1].split("@")[0]);
使用字符串。拆分:

 String a = "Local/5028@from-queue-bd7f,1";
         Pattern p = Pattern.compile("\\d+");
         Matcher m = p.matcher(a);
         System.out.println(m.find() + " " + m.group());
String a = "Local/5028@from-queue-bd7f,1";
         String[] split = a.split("/");
         System.out.println(split[1].split("@")[0]);

使用正则表达式:

 String a = "Local/5028@from-queue-bd7f,1";
         Pattern p = Pattern.compile("\\d+");
         Matcher m = p.matcher(a);
         System.out.println(m.find() + " " + m.group());
String a = "Local/5028@from-queue-bd7f,1";
         String[] split = a.split("/");
         System.out.println(split[1].split("@")[0]);
使用字符串。拆分:

 String a = "Local/5028@from-queue-bd7f,1";
         Pattern p = Pattern.compile("\\d+");
         Matcher m = p.matcher(a);
         System.out.println(m.find() + " " + m.group());
String a = "Local/5028@from-queue-bd7f,1";
         String[] split = a.split("/");
         System.out.println(split[1].split("@")[0]);

如果字符串格式是固定的,则可以使用:

String a = "Local/5028@from-queue-bd7f,1";
a = a.substring(a.indexOf('/') + 1, a.indexOf('@'));
System.out.println(a);

如果字符串格式是固定的,则可以使用:

String a = "Local/5028@from-queue-bd7f,1";
a = a.substring(a.indexOf('/') + 1, a.indexOf('@'));
System.out.println(a);

你试过什么吗?例如RegExp?值总是从同一个位置开始,还是总是在
/
@
之间,或者这是一个完全不同的规则?先生,我试着通过拆分字符串值来实现这一点。它将引导您完成整个过程。您知道正则表达式吗?似乎是你的朋友你试过什么吗?例如RegExp?值总是从同一个位置开始,还是总是在
/
@
之间,或者这是一个完全不同的规则?先生,我试着通过拆分字符串值来实现这一点。它将引导您完成整个过程。您知道正则表达式吗?在这种情况下,似乎是你的朋友。-1表示允许使用幻数,而不是真正的通用解决方案。它也是一个容易出错的解决方案。-1表示允许使用幻数,而不是真正的通用解决方案。这也是一个容易出错的解决方案;请告诉我如何在代码中再次组合此条件如果字符串是固定的,请尝试
a.substring(a.indexOf('/')+1,a.indexOf(“from”)-1)
要处理这两种情况,我还有一个case字符串a=“Local/5028-from-queue-bd7f,1”;请告诉我如何在代码中再次组合此条件如果字符串是固定的,请尝试
a.substring(a.indexOf('/')+1,a.indexOf(“from”)-1)
来处理这两个条件