Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove matched words from end of String

Tags:

java

I want to remove the following words from end of String ‘PTE’, ‘LTD’, ‘PRIVATE’ and ‘LIMITED’

i tried the code but then i stuck. i tried this

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

String company = "Basit LTD";
for(int i=0;i<str.length;i++) {

    if (company.endsWith(str[i])) {

        int position = company.lastIndexOf(str[i]);
        company = company.substring(0, position);        
    }
}

System.out.println(company.replaceAll("\\s",""));

It worked. But suppose the company is Basit LIMITED PRIVATE LTD PTE or Basit LIMITED PRIVATE PTE LTD or any combination of four words in the end. Then the above code just remove the last name i.e., PTE or PRIVATE and so on, and the output is BasitLIMITEDPRIVATELTD.

I want output to be just Basit

How can i do it?

Thanks

---------------Edit---

Please note here the company name is just an example, it is not necessary that it is always the same. may be i have name like

String company = "Masood LIMITED LTD PTE PRIVATE"

or any name that can have the above mentioned words at the end.

Thanks

like image 778
Basit Avatar asked Mar 07 '13 09:03

Basit


People also ask

How do I remove the words from the end of a string in word?

To remove the last word from a string, get the index of the last space in the string, using the lastIndexOf() method. Then use the substring() method to get a portion of the string with the last word removed.

How do I remove the last text from a string?

Using String. The easiest way is to use the built-in substring() method of the String class. In order to remove the last character of a given String, we have to use two parameters: 0 as the starting index, and the index of the penultimate character.

How do I remove the last two words from a string?

To remove the last 2 words from a string: Call the split() method on the string to split it into an array of words. Use the slice method to get an array excluding the last 2 words of the string. Call the join() method on the array and join the words with a space.


2 Answers

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$*?","");     
like image 70
PermGenError Avatar answered Sep 28 '22 16:09

PermGenError


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);
like image 36
user_CC Avatar answered Sep 28 '22 16:09

user_CC