How to remove matching words from the end of a line

I want to remove the following words from the end of the line 'PTE', 'LTD', 'PRIVATE' and 'LIMITED'

I tried the code but then got 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 Basit LIMITED PRIVATE LTD PTE or Basit LIMITED PRIVATE PTE LTD or any four-word combination at the end. Then the above code just removes the last name, i.e. PTE or PRIVATE , etc., and the output is BasitLIMITEDPRIVATELTD .

I want the output to be just Basit

How can i do this?

thanks

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

Please note that the name of the company is just an example, it is not necessary that it is always the same. maybe i have a name like

 String company = "Masood LIMITED LTD PTE PRIVATE" 

or any name that may contain the above words at the end.

thanks

+4
source share
7 answers

If you put unwanted words on the card, it will be omitted in the resulting line

  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); 
+2
source

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

 company = company.replaceAll("PTE$*?|LTD$*?|PRIVATE$*?|LIMITED$*?",""); 
+9
source

If you want to remove these suffixes only at the end of the line, you can enter a while :

 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 do not mind converting PTE Basit LIMITED INC to Basit (and also remove the first PTE), then replaceAll should work, as others have explained.

+2
source

Try the following:

 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","")); } 
0
source

All you need to do is use trim() and call your function recursively, or every time you remove a substring from the end, reset your i to 0.

0
source
 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); } 

using the replacement method first, we get word=my name is ..... khan (Note: here ( . ) represents a space). Now we need to remove these spaces so that we create a new line, adding all tokens is simple.

Exit: my name is khan

0
source

I tried to do the same for one of my projects. I wrote this code a few days ago. Now I was definitely trying to find a much better way to do this, as I found this question. But, 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()); 
0
source

All Articles