StringTokenizer Usage 复习

新写的排序EPC list的方法,利用StringTokenizer和Collections.sort,复习+备份用哈。

/**
* Example SOAP response:
*
* <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Header/><soapenv:Body><p559:GetImmediateChildrenResponse xmlns:p559="http://www.ibm.com/epcis/pedigree/TrackAndTraceService/1.5"><epc>urn:epc:id:sgtin:0121212.111111.05 urn:epc:id:sgtin:0121212.111111.01 urn:epc:id:sgtin:0121212.111111.06</epc></p559:GetImmediateChildrenResponse></soapenv:Body></soapenv:Envelope>
*
* If orginXml like "3 1 2", then return will be "1 2 3"
*
* @param orgin
* @return
*/
public String sortEpcTag(String orginXml){
if (orginXml.contains("<epc>"))
{
System.out.println("Response file contains <epc> tag, sort the EPC list inside it");
String epcList = orginXml.split("<epc>")[1].split("</epc>")[0];
StringTokenizer st = new StringTokenizer(epcList);
LinkedList sortedEpcList = new LinkedList();
while( st.hasMoreElements() ){
sortedEpcList.add(st.nextToken());
}
Collections.sort(sortedEpcList);
StringBuilder temp = new StringBuilder();
Iterator iter = sortedEpcList.iterator();
while(iter.hasNext()){
temp.append(iter.next());
if(iter.hasNext()){
temp.append(" ");
}
}
return orginXml.replaceAll(epcList, temp.toString());
}
return orginXml;
}
 
public static void main (String[] args) {
String actual = "<epc>urn:epc:id:sgtin:0121212.111111.05 urn:epc:id:sgtin:0121212.111111.01 urn:epc:id:sgtin:0121212.111111.06</epc>";
String expect = "<epc>urn:epc:id:sgtin:0121212.111111.01 urn:epc:id:sgtin:0121212.111111.06 urn:epc:id:sgtin:0121212.111111.04</epc>";
String afterSortActual = new ValidateResponseForConfig53().sortEpcTag(actual);
String afterSortExpect = new ValidateResponseForConfig53().sortEpcTag(expect);
 
System.out.println(afterSortActual);
System.out.println(afterSortExpect);
if (afterSortExpect.equals(afterSortActual)){
System.out.println("equal");
}
else
{
System.out.println("Not equal");
}
 
}

以下内容为转载:

StringTokenizer:字符串分隔解析类型
属于:java.util包。

1、构造函数。

    1. StringTokenizer(String str) :构造一个用来解析str的StringTokenizer对象。java默认的分隔符是“空格”、“制表符(‘\t’)”、“换行符(‘\n’)”、“回车符(‘\r’)”。
    2. StringTokenizer(String str, String delim) :构造一个用来解析str的StringTokenizer对象,并提供一个指定的分隔符。
    3. StringTokenizer(String str, String delim, boolean returnDelims) :构造一个用来解析str的StringTokenizer对象,并提供一个指定的分隔符,同时,指定是否返回分隔符。

2、方法。

    1. int countTokens() :返回nextToken方法被调用的次数。如果采用构造函数1和2,返回的就是分隔符数量(例2)。
    2. boolean hasMoreTokens() :返回是否还有分隔符。
    3. boolean hasMoreElements() :结果同2。
    4. String nextToken() :返回从当前位置到下一个分隔符的字符串。
    5. Object nextElement() :结果同4。
    6. String nextToken(String delim) :与4类似,以指定的分隔符返回结果。

例1:

		String s = new String("The Java platform is the ideal platform for network computing");
		StringTokenizer st = new StringTokenizer(s);
		System.out.println( "Token Total: " + st.countTokens() );
		while( st.hasMoreElements() ){
			System.out.println( st.nextToken() );
           }

结果为:
Token Total: 10
The
Java
platform
is
the
ideal
platform
for
network
computing

例2:

	String s = new String("The=Java=platform=is=the=ideal=platform=for=network=computing");
		StringTokenizer st = new StringTokenizer(s,"=",true);
		System.out.println( "Token Total: " + st.countTokens() );
		while( st.hasMoreElements() ){
			System.out.println( st.nextToken() );
		}

结果为:
Token Total: 19
The
=
Java
=
platform
=
is
=
the
=
ideal
=
platform
=
for
=
network
=
computing

    Leave a Comment.