当前位置: 技术问答>java相关
怎样比较String中的部份是否相等?
来源: 互联网 发布时间:2015-04-02
本文导语: 我有四个String "person1,chinese", "persona12,british", "persona113,american", "person2224,chinese", 我想比较一下这四个string中,有几个人来自相同的国家。charAt和compareto在这里都不好用,因为每个String中第一个单词的字数都不一样。...
我有四个String
"person1,chinese",
"persona12,british",
"persona113,american",
"person2224,chinese",
我想比较一下这四个string中,有几个人来自相同的国家。charAt和compareto在这里都不好用,因为每个String中第一个单词的字数都不一样。用什么办法可以把String中的每个单词给分开,然后在比较。
谢谢
"person1,chinese",
"persona12,british",
"persona113,american",
"person2224,chinese",
我想比较一下这四个string中,有几个人来自相同的国家。charAt和compareto在这里都不好用,因为每个String中第一个单词的字数都不一样。用什么办法可以把String中的每个单词给分开,然后在比较。
谢谢
|
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 names
here 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 =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 0? ",":"") + v.get(i));
System.out.println();
}
}
}
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 names
here 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 =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 0? ",":"") + v.get(i));
System.out.println();
}
}
}
|
StringTokenizer
StringTokenizer st = new StringTokenizer("persona12,british", ',");
println(st.nextToken());
StringTokenizer st = new StringTokenizer("persona12,british", ',");
println(st.nextToken());
|
或者用String.indexOf()
|
you can use lastIndexOf method
it return the position of the last occurrence of str within this string... example string t1="person1 china"; t1.lastIndexOf("china") you get it
it return the position of the last occurrence of str within this string... example string t1="person1 china"; t1.lastIndexOf("china") you get it
|
可以先用String.indexOf()找出","的位置
然后找","之后的子串 进行比较
然后找","之后的子串 进行比较
|
只要每个String里都会有逗号作为分割符,事情就好办(用IndexOf()或lastIndexOf()都行)