StringTokenizer
     StringTokenizer st = new StringTokenizer("persona12,british", ',");
         println(st.nextToken());
     
 

解决方案 »

  1.   

    1. create a Hashtable which maps String to a Vector
    2. split the Strings into two parts, person, country, use the country as a key to add the person to the Vector
    3. iterate through the keys (countries) of the HashTable, write out the nameshere is a sample implementation (TestPerson.java):import java.util.*;public class TestPerson
    {
       public static void main(String[] args)
       {
    String[] people = {"person1,chinese", "persona12,british","persona113,american","person2224,chinese"}; Hashtable ht = new Hashtable();
    String person, country;
    int i;
    Vector v; for (i=0; i < people.length;i++)
    {
    int n=people[i].indexOf(',');
    if (n >=0)
    {
    person = people[i].substring(0,n);
    country = people[i].substring(n+1);

    if (!ht.containsKey(country))
    {
    v = new Vector();
    ht.put(country,v);
    } v = (Vector) ht.get(country);
    v.add(person);
    }
    } for (Enumeration e = ht.keys() ; e.hasMoreElements() ;) 
    {
    country = (String)e.nextElement(); System.out.print("The following people are " + country + ":"); v = (Vector)ht.get(country);

    for (i=0; i < v.size(); i++)
    System.out.print((i > 0? ",":"") + v.get(i)); System.out.println();
             
          }
     
       }
    }
      

  2.   

    you can use lastIndexOf methodit return the position of the last occurrence of str within this string...     example string t1="person1 china";  t1.lastIndexOf("china")  you get it
      

  3.   

    可以先用String.indexOf()找出","的位置
    然后找","之后的子串  进行比较
      

  4.   

    只要每个String里都会有逗号作为分割符,事情就好办(用IndexOf()或lastIndexOf()都行)