java中的重复分隔符与数组的损坏消息

Posted

技术标签:

【中文标题】java中的重复分隔符与数组的损坏消息【英文标题】:Recurring Delimiter in java with broken message to an array 【发布时间】:2017-09-10 07:58:51 【问题描述】:

我有一个字符串:

String received = "28,gZrate:-1.48,
gXrate:9.10,gYrate:-0.69,gZrate:-1.50,        
gXrate:9.22,gYrate:0.54,gZrate:-1.67,     
gXrate:9.20,gYrate:-2.40,gZrate:-1.65,gXrate:9.02" ; 

我需要这个字符串在这样的数组中:

["gXrate:9.10,gYrate:-0.69,gZrate:-1.50","gXrate:9.22,gYrate:0.54,gZrate:-1.67","gXrate:9.20,gYrate:-2.40,gZrate:-1.65"]

忽略接收到的字符串开头和结尾的断线部分 即

28,gZrate:-1.48

gXrate:9.02 ; 

还要注意有时字符串可能不会被破坏。

【问题讨论】:

【参考方案1】:
String received = "28,gZrate:-1.48,gXrate:9.10,gYrate:-0.69,gZrate:-1.50,";
       received += "gXrate:9.22,gYrate:0.54,gZrate:-1.67,";
       received += "gXrate:9.20,gYrate:-2.40,gZrate:-1.65,gXrate:9.02";
String pattern = "\\(.*?)\\,?";
StringBuilder sb = new StringBuilder("[");
boolean first = true;

Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(received);
while (m.find()) 
    if (!first) 
        sb.append(",");
    
    first = false;
    sb.append("\"" + m.group(1) + "\"");

sb.append("]");
System.out.println(sb);

输出:

["gXrate:9.10,gYrate:-0.69,gZrate:-1.50","gXrate:9.22,gYrate:0.54,gZrate:-1.67","gXrate:9.20,gYrate:-2.40,gZrate:-1.65"]

这是一个演示,它表明上述代码适用于破碎和平衡的情况:

Rextester

【讨论】:

【参考方案2】:

你可以使用正则表达式:

import java.util.regex.*;
import java.util.Arrays;
import java.util.ArrayList;

public class Main 
  public static void main (String[] args) 
    ArrayList<String> nonBrokenStrings = new ArrayList<String>();
    String received = "28,gZrate:-1.48,gXrate:9.10,gYrate:-0.69,gZrate:-1.50, gXrate:9.22,gYrate:0.54,gZrate:-1.67, gXrate:9.20,gYrate:-2.40,gZrate:-1.65,gXrate:9.02";
    Matcher m = Pattern.compile("\\([^]+)\\").matcher(received);
    while(m.find()) 
      nonBrokenStrings.add(m.group(1));
    
    String[] nonBrokenStringsArr = nonBrokenStrings.toArray(new String[nonBrokenStrings.size()]);
    System.out.print(Arrays.toString(nonBrokenStringsArr));
  

输出:

[gXrate:9.10,gYrate:-0.69,gZrate:-1.50, gXrate:9.22,gYrate:0.54,gZrate:-1.67, gXrate:9.20,gYrate:-2.40,gZrate:-1.65] 

试试here!

【讨论】:

【参考方案3】:
public static String formatToArrayStr(String input)
    String regex="\\([^]+)\\";
    Matcher m=Pattern.compile(regex).matcher(input);
    StringBuilder result=new StringBuilder().append("[");
    while(m.find())
        result.append("\"").append(m.group(1)).append("\"").append(",");
           
    result=(result.lastIndexOf(",")>=0?result.deleteCharAt(result.length()-1):result).append("]");
    return result.toString();

也可以这样写:

public static String formatToArrayStr(String input)
    Matcher m=Pattern.compile("\\([^]+)\\").matcher(input);
    List<String> result=new ArrayList<String>();
    while(m.find())
        result.add("\""+m.group(1)+"\"");
           
    return result.toString();

输出

["gXrate:9.10,gYrate:-0.69,gZrate:-1.50","gXrate:9.22,gYrate:0.54,gZrate:-1.67","gXrate:9.20,gYrate:-2.40,gZrate:-1.65"]

【讨论】:

以上是关于java中的重复分隔符与数组的损坏消息的主要内容,如果未能解决你的问题,请参考以下文章

将制表符分隔的文本转换为数组[重复]

使用 LINQ/lambdas 构建以逗号分隔的内部异常消息字符串 [重复]

如何拆分字符串但在java中保留分隔符? [复制]

Java:使用正则表达式拆分字符串而不删除分隔符[重复]

检查字符串(句子空格分隔)以查看它是不是包含数组中的任何字符

如何使用分隔符数组在 Java 中使用多个分隔符拆分字符串? [复制]