将连续的字符串数据拆分为所需的垂直输出

时间:2019-07-09 03:00:52

标签: java html css regex jsp

我正在使用scriplet在jsp页面中显示输出。

我正在从数据库获取输出,如下所示:

  out.println(a) ; //prints output Jan-2019 Feb-2019 March-2019 April-2019

  out.println(b) ; //prints output 100100200300

我正尝试使用html css在jsp页面中打印输出,如下所示:

    Month           Price
    Jan-2019         100
    Feb-2019         100
    March-2019       200
    April-2019       300

我在Google中搜索了很多内容,但仍未找到任何解决方案,也尝试过使用其他正则表达式代码进行解析,但仍未解决。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

要拆分a,请执行以下操作:

String[] months = a.split(" ");

要拆分b,请执行以下操作:

ArrayList<String> prices = new ArrayList<String>();
boolean isZero = false;
String tmpPrice = "";
for (int i = 0; i < b.length(); i++) {
    if (i + 1 >= b.length()) {
        tmpPrice = tmpPrice + b.charAt(i);
        prices.add(tmpPrice);
    } else if (isZero && b.charAt(i) != '0') {
        prices.add(tmpPrice);
        tmpPrice = "" + b.charAt(i);
        isZero = false;
    } else if (b.charAt(i) == '0') {
        isZero = true;
        tmpPrice = tmpPrice + b.charAt(i);
    } else if (b.charAt(i) != '0') {
        isZero = false;
        tmpPrice = tmpPrice + b.charAt(i);
    }
}

答案 1 :(得分:1)

这是代码。故意使它变得更“冗长”,以便在数据Feed中更方便地使用正则表达式。

String a = "Jan-2019 Feb-2019 March-2019 April-2019";
String b = "100100200300";

Pattern P1 = Pattern.compile("(\\w{3,})-(\\d{4})");
Pattern P2 = Pattern.compile("[1-9]+0+");

Vector<Integer> years = new Vector<>();
Vector<String> months = new Vector<>();
Vector<Integer> prices = new Vector<>();

Matcher m = P1.matcher(a);
while (m.find())
{
    months.add(m.group(1));
    years.add(new Integer(m.group(2)));
}

m = P2.matcher(b);
while (m.find()) prices.add(new Integer(m.group()));

// Useful for debugging, to make sure these are "parallel arrays"
// Parallel Arrays are almost *always* useful when regular-expressions & parsing is "happening."
System.out.println( "years.size():\t" + years.size() + '\n' +
                    "months.size():\t" + months.size() + '\n' +
                    "prices.size():\t" + prices.size() + "\n\n"   );

int len = years.size();
for (int i=0; i < len; i++)
    System.out.println( months.elementAt(i) + " " +
                        years.elementAt(i) + ":\t" +
                        prices.elementAt(i) );

System.exit(0);

以下是输出:

years.size():   4
months.size():  4
prices.size():  4


Jan 2019:       100
Feb 2019:       100
March 2019:     200
April 2019:     300