Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9246861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:35:33+00:00 2026-06-18T09:35:33+00:00

I am using Java, i need your opinion on how to write better code

  • 0

I am using Java, i need your opinion on how to write better code for the following task.

I have following String value

String testStr = "INCLUDES(ABC) EXCLUDES(ABC) EXCLUDES(ABC) INCLUDES(ABC) INCLUDES(ABC)"

I want to manipulate Strings and want to combine all INCLUDES statements into one INCLUDES and the result should be similar to the following:

INCLUDES(ABC,ABC, ABC) EXCLUDES(ABC, ABC)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:35:35+00:00Added an answer on June 18, 2026 at 9:35 am

    I have wrote down small utility result as following

    if text = "EXCLUDES(ABC) EXCLUDES(ABC) INCLUDES(BMG) INCLUDES(EFG) INCLUDES(IJK)";
    
    output = EXCLUDES(ABC) EXCLUDES(ABC) INCLUDES(BMG & EFG & IJK)
    

    Following is my java codeas following please take a look and if any one can improve it please feel free.

    import java.util.ArrayList;
    import java.util.List;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    import com.sun.xml.internal.ws.util.StringUtils;
    
    /**
     * Created by IntelliJ IDEA.
     * User: fkhan
     * Date: Aug 31, 2012
     * Time: 1:36:45 PM
     * To change this template use File | Settings | File Templates.
     */
    
    
    public class TestClass {
    
    
        public static void main(String args[]) throws Exception {
    
            //String text = "INCLUDES(ABC) EXCLUDES(ABC) EXCLUDES(ABC) INCLUDES(EFG) INCLUDES(IJK)";
            String text = "EXCLUDES(ABC) EXCLUDES(ABC) INCLUDES(BMG) INCLUDES(EFG) INCLUDES(IJK)";
            List<String> matchedList = findMatchPhrase("INCLUDES", text);
            String query = combinePhrase(text, "INCLUDES", matchedList);
            System.out.println(query);
    
    
        }
    
        /**
         * This method takes query combine and & multiple phrases
         * @param expression
         * @param keyword
         * @param matchedItemList
         * @return
         */
        private static String combinePhrase(String expression, String keyword, List<String> matchedItemList) {
    
            //if only one phrase found return value
            if(matchedItemList.isEmpty() || matchedItemList.size() ==1){
                return expression;
            }
    
            //do not remove first match
            String matchedItem = null;
            for (int index = 1; index < matchedItemList.size(); index++) {
    
                matchedItem = matchedItemList.get(index);
    
                //remove match items from string other then first match
                expression = expression.replace(matchedItem, "");
            }
    
            StringBuffer textBuffer = new StringBuffer(expression);
    
            //combine other matched strings in first matched item
            StringBuffer combineStrBuf = new StringBuffer();
            if (matchedItemList.size() > 1) {
    
                for (int index = 1; index < matchedItemList.size(); index++) {
                    String str = matchedItemList.get(index);
                    combineStrBuf.append((parseValue(keyword, str)));
                    combineStrBuf.append(" & ");
    
                }
                combineStrBuf.delete(combineStrBuf.lastIndexOf(" & "), combineStrBuf.length());
            }
    
            // Inject created phrase into first phrase
            //append in existing phrase
            return injectInPhrase(keyword, textBuffer, combineStrBuf.toString());
        }
    
        /**
         *
         * @param keyword
         * @param textBuffer
         * @param injectStr
         */
        private static String injectInPhrase(String keyword, StringBuffer textBuffer, String injectStr) {
            Matcher matcher = getMatcher(textBuffer.toString());
            while (matcher.find()) {
    
                String subStr = matcher.group();
                if (subStr.startsWith(keyword)) {
                    textBuffer.insert(matcher.end()-1, " & ".concat(injectStr));
                    break;
                }
    
            }
    
           return textBuffer.toString();
        }
    
        /**
         * @param expression
         * @param keyword
         * @return
         */
        private static String parseValue(String keyword, String expression) {
    
            String parsStr = "";
            if (expression.indexOf(keyword) > -1) {
                parsStr = expression.replace(keyword, "").replace("(", "").replace(")", "");
            }
    
            return parsStr;
        }
    
    
        /**
         * This method creates matcher object
         * and return for further processing
         * @param expression
         * @return
         */
        private static Matcher getMatcher(String expression){
            String patternString = "(\\w+)\\((.*?)\\)";
            Pattern pattern = Pattern.compile(patternString);
            return pattern.matcher(expression);
        }
        /**
         * This method find find matched items by keyword
         * and return as list
         * @param keyword
         * @param expression
         * @return
         */
        private static List<String> findMatchPhrase(String keyword, String expression) {
            List<String> matchList = new ArrayList<String>(3);
    
            keyword = StringUtils.capitalize(keyword);
            Matcher matcher = getMatcher(expression);
    
            while (matcher.find()) {
    
                String subStr = matcher.group();
                if (subStr.startsWith(keyword)) {
                    matchList.add(subStr);
                }
            }
    
            return matchList;
        }
    
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to get the source code of the particular URL using a java
I'm new to Java and really need your help. I am presently using a
I need to create a prn file using java. Did a basic googling to
I need to create 1000 server sockets using Java. Somewhere between creating 600 and
Situation: I need to make an imap client (using java mail api) that if,
I need to insert Chinese characters inside Oracle Data base using Java (hibernate 3.0
I need to detect all the red pixels in an image using Java. What
i need to send and receive data to/from a microcontroller using java using usb
I need to execute the given commands as root and sudo user using Java.
i'm using sqlitejdbc libraries to create a db using java. Now i need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.