Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java分割数据以获取值_Java - Fatal编程技术网

使用java分割数据以获取值

使用java分割数据以获取值,java,Java,我有一个字符串中的回复消息,我想将其拆分以提取一个值 我的字符串回复消息格式为: REPLY(MSGID,15,ABC024049364194,SERVICE,10,CREATE,...); 我的要求是从上述字符串格式中获取值ABC024049364194 我尝试使用此代码,但不起作用: String[] arrOfStr = str.split(",", 5); for (String a : arrOfStr) System.out.println(a); 如果您的代码始终

我有一个
字符串中的回复消息,我想将其拆分以提取一个值

我的字符串回复消息格式为:

REPLY(MSGID,15,ABC024049364194,SERVICE,10,CREATE,...);
我的要求是从上述字符串格式中获取值
ABC024049364194

我尝试使用此代码,但不起作用:

String[] arrOfStr = str.split(",", 5); 

for (String a : arrOfStr) 
    System.out.println(a);

如果您的代码始终在2之后,则将起作用。如果拆分字符串,只需访问索引2处的令牌即可

// <TYPE>(<ARGUMENTS>)
String message = "REPLY(MSGID,15,ABC024049364194,SERVICE,10,CREATE);";
Matcher m = Pattern.compile("(\\w+)\\((.+)\\)").matcher(message);
if (m.find()) {
    String type = m.group(1);
    String[] arguments = m.group(2).split(",");
    System.out.println(type + " = " + arguments[2]); // REPLY = ABC024049364194
}
//()
String message=“回复(MSGID,15,ABC024049364194,服务,10,创建);”;
Matcher m=Pattern.compile(“(\\w+)\\(.+)\\”).Matcher(消息);
if(m.find()){
字符串类型=m.组(1);
String[]arguments=m.group(2).split(“,”);
System.out.println(type+“=”+参数[2]);//REPLY=ABC024049364194
}

如果用逗号拆分
字符串
,则之后必须获取正确的索引。就我所见,它将在索引2上,所以做
String[]arr=str.split(“,”);System.out.println(arr[2])定义“它没有工作”。请看这一页。这就是说,如果你有这样的普遍需要,regex并不总是最好的解决方案。嗨,什么意思是它不起作用?请描述你的行为或错误,从第一眼看你是在正确的道路上,如果你只想要ABC。。值,那么您需要第二个索引上的标记,索引是从零开始的,因此第一个
[0]
回复(MSGID
[1]15
[2]ABC024049364194
// <TYPE>(<ARGUMENTS>)
String message = "REPLY(MSGID,15,ABC024049364194,SERVICE,10,CREATE);";
Matcher m = Pattern.compile("(\\w+)\\((.+)\\)").matcher(message);
if (m.find()) {
    String type = m.group(1);
    String[] arguments = m.group(2).split(",");
    System.out.println(type + " = " + arguments[2]); // REPLY = ABC024049364194
}