2014年5月22日星期四

Great God seeking help with java to write a function that parse a string , thank you !

string format :
var hq_str_sz000001 = " Ping An Bank , 11.20,11.24,11.33,11.38,11.08,11.32,11.33,26316829,297218362.63,375042,11.32,274513,11.31,394900,11.30,302500,11.29,62200,11.28 ,334526,11.33,263977,11.34,311051,11.35,155089,11.36,256850,11.37,2014 -05-21 , 15:05:38,00 " ;

pieces of content using commas , and asked to use a function to achieve , is to read into the string str, resolve after getting a str [] array, and output in this format :

Stock Code : sz000001
Stock Name : Ping An Bank
Today Open: 11.20
yesterday's closing price : 11.24
Current Price: 11.33
Today's highest price : 11.38
Day Low: 11.08
bid price : 11.32
auctions Price : 11.33
number of shares traded : 26,316,829
Turnover : 297,218,362.63
buy a ( dollar ) / Hand : 375042
buy a ( dollar ) / hand : 11.32
buy two ( RMB ) / Hand : 274513
buy two ( RMB ) / hand : 11.31
Buy 3 ( RMB ) / Hand : 394900
Buy 3 ( RMB ) / hand : 11.30
buy four ( RMB ) / Hand : 302500
buy four ( RMB ) / hand : 11.29
buy five ( RMB ) / Hand : 62200
buy five ( RMB ) / hand : 11.28
sell a ( dollar ) / Hand : 334526
sell a ( dollar ) / hand : 11.33
sell two ( RMB ) / Hand : 263977
sell two ( RMB ) / hand : 11.34
sell three ( RMB ) / Hand : 311051
sell three ( RMB ) / hand : 11.35
sell four ( RMB ) / Hand : 155089
sell four ( RMB ) / hand : 11.36
sell five ( RMB ) / Hand : 256850
sell five ( RMB ) / hand : 11.37
Date :2014 -05-21
Time: 15:05:38

I know there are split this function, but specifically how to get the outcome is unlikely , I hope the great God who help out, thank you !

In addition, the portion of the string , including the left side of the equal sign , above a whole need to parse strings
------ Solution -------------- ------------------------------
direct
String [] ss = hq_str_sz000001.split (",");
then to the front of each element on the line with the corresponding string
such as ss [0] = " Stock Name :" + ss [0];
------ Solution ------------- -------------------------------
chant by a comma-separated into an array
then traverse the array , put together at the beginning !
This is the JS right you determine to use java to do ?

------ Solution ------------------------------------ --------
looks like Sina or Sohu 's stock api
------ Solution -------------------------------- ------------
is js or background ah.

can fight themselves.
------ Solution ---------------------------------------- ----
code is as follows :
package test;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class test {

public static void main(String args[])
{
List<String> list = new ArrayList<String>();
String str = "var hq_str_sz000001=\"平安银行,11.20,11.24,11.33,11.38,11.08,11.32,11.33,26316829,297218362.63,375042,11.32,274513,11.31,394900,11.30,302500,11.29,62200,11.28,334526,11.33,263977,11.34,311051,11.35,155089,11.36,256850,11.37,2014-05-21,15:05:38,00\";";
String[] temp = str.split(",");
Pattern pattern = Pattern.compile(".*_([a-z]{2}\\d{6})=\"(.*)$");
Matcher matcher = pattern.matcher(temp[0]);
if(matcher.find())
{
list.add(matcher.group(1));
list.add(matcher.group(2));
}
for(int i=2; i<temp.length; i++)
list.add(temp[i]);
for(int j=0; j<list.size()-1; j++)
System.out.println(list.get(j));
}
}


output: ( please add those names themselves )
sz000001
平安银行
11.24
11.33
11.38
11.08
11.32
11.33
26316829
297218362.63
375042
11.32
274513
11.31
394900
11.30
302500
11.29
62200
11.28
334526
11.33
263977
11.34
311051
11.35
155089
11.36
256850
11.37
2014-05-21
15:05:38

------ Solution ------------------------------------- -------
give you a way to intercept the string
package test;

public class Test {

    public static String intercept(String str, String begin, String end, String splitStr) {
        String result = "";
        int i;
        int j = 0;
        do {
            i = str.indexOf(str, j);
            if (i == -1) {
                break;
            }
            i += begin.length();
            j = str.indexOf(end);
            if (j == -1) {
                break;
            }
            if (j > 0) {
                if (result.length() > 0) {
                    result += str.substring(i, j);
                } else {
                    result = splitStr + str.substring(i, j);
                }
            }
        } while (true);
        return result;
    }

    public static void main(String[] args) {
        String str = "var hq_str_sz000001=\"平安银行,11.20,11.24,11.33,11.38,11.08,11.32,11.33,26316829,297218362.63,375042,11.32,274513,11.31,394900,11.30,302500,11.29,62200,11.28,334526,11.33,263977,11.34,311051,11.35,155089,11.36,256850,11.37,2014-05-21,15:05:38,00\";";
        String reString = intercept(str, "var hq_str_sz000001=\"", "\";", ",");
        System.out.println(reString);
    }
}

then follow a floor to handle
------ Solution ---------------------------- ----------------


seeking more to the point of
------ For reference only ---------------------------------------
cry ...... in front of the hq_str_sz000001 = also the string , and a whole are on top of . I just do not know in front of the space, underscore , equal sign quotes how well these treatments , I am now doing is parsing the returned http top of this string, and then the following side output format
------ For reference only ---------------------------------------
right, I am now doing Andrews is a software , you need to get data through the API, and then show up
------ For reference only ---------------------- -----------------
thanks so much ! !

没有评论:

发表评论