5
4

7 回答 7

9

You can do this in single line. no need to loop through. just use String#replaceAll(regex, str).

company = company.replaceAll("PTE$*?|LTD$*?|PRIVATE$*?|LIMITED$*?","");     
于 2013-03-07T09:33:53.650 回答
2

If you want to remove these suffixes only at the end of the string, then you could introduce a while loop:

String[] str = {"PTE", "LTD", "PRIVATE", "LIMITED"};
boolean foundSuffix = true;

String company = "Basit LTD";

while (foundSuffix) {
    foundSuffix = false;

    for(int i=0;i<str.length;i++) {

        if (company.endsWith(str[i])) {
            foundSuffix = true;
            int position = company.lastIndexOf(str[i]);
            company = company.substring(0, position);        
        }
    }
}
System.out.println(company.replaceAll("\\s",""));

If you don't mind transforming PTE Basit LIMITED INC to Basit (and also remove the first PTE), then replaceAll should work, as explained by others.

于 2013-03-07T09:39:07.190 回答
2

If you place the unwanted words in the map it will be ommitted in the resultant string

    HashMap map = new HashMap();
    map.put("PTE", "");
    map.put("LTD", "");
    map.put("PRIVATE", "");
    map.put("LIMITED", "");



    String company = "Basit LTD PRIVATE PTE";


    String words[] = company.split(" ");

    String resultantStr = "";

    for(int k = 0; k < words.length; k++){
        if(map.get(words[k]) == null) {
            resultantStr += words[k] + " ";

        }
    }


    resultantStr = resultantStr.trim();
    System.out.println(" Trimmed String: "+  resultantStr);
于 2013-03-07T09:47:24.537 回答
1

I was trying to do exactly same thing for one of my projects. I wrote this code few days earlier. Now I was exactly trying to find a much better way to do it, that's how I found this Question. But after seeing other answers I decided to share my version of the code.

Collection<String> stopWordSet = Arrays.asList("PTE", "LTD", "PRIVATE", "LIMITED");    

String company = "Basit LTD"; //Or Anything
String[] tokens = company.split("[\@\]\\\_\^\[\"\#\ \!\&\'\`\$\%\*\+\(\)\.\/\,\-\;\~\:\}\|\{\?\>\=\<]+");

Stack<String> tokenStack = new Stack<>();
tokenStack.addAll(Arrays.asList(tokens));
while (!tokenStack.isEmpty()) {
     String token = tokenStack.peek();
     if (stopWordSet.contains(token))
         tokenStack.pop();
     else
         break;
}

String formattedCompanyName = StringUtils.join(tokenStack.toArray());
于 2015-02-16T13:15:57.783 回答
0

Try this :

public static void main(String a[]) {   
    String[] str = {"PTE", "LTD", "PRIVATE", "LIMITED"};
    String company = "Basit LIMITED PRIVATE LTD PTE";

    for(int i=0;i<str.length;i++) {
        company = company.replaceAll(str[i], "");
    }

    System.out.println(company.replaceAll("\\s",""));
}
于 2013-03-07T09:37:20.333 回答
0

All you need is to use trim() and call your function recursively, Or each time you remove a sub string from the end, reset your i to 0.

于 2013-03-07T09:37:40.563 回答
0
public class StringMatchRemove {

    public static void main(String[] args) {

        String str="my name is noorus khan";
        String search="noorus";
        String newString="";
        String word=str.replace(search," ");
        StringTokenizer st = new StringTokenizer(word," ");
        while(st.hasMoreTokens())
        {
          newString = newString + st.nextToken() + " "; 
        }
        System.out.println(newString);

    }

first using the replace method we get word=my name is ..... khan (Note: here(.) represents the space). Now we should have to remove these spaces for that we are creating a new string adding all the token simply.

Output: my name is khan

于 2014-02-25T18:37:43.113 回答