Advertisement

07.24.2007 at 08:49PM PDT, ID: 22718953
[x]
Attachment Details

Split a String based on regular expression

Asked by Suda_RamanaReddy in Java Server Pages (JSP)

Tags: split, string, based, expression, regular

Hi All,

I have a simple query.....

 * @author RamanaReddy Suda
 */
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Splitter {
    private int maxLen;
    private int maxCount;
    private int minCount;

    private static int defaultMaxLen = 32*1024;
    private static int defaultMaxCount = 12;
    private static int defaultMinCount = 2;

    // default constructor
    public Splitter() {
        maxCount = defaultMaxCount;
        minCount = defaultMinCount;
          maxLen = defaultMaxLen;
    }

//  custom constructor: make new Splitter with specified maxLen value
    public Splitter(int maxLen) {
        this.maxLen = maxLen;
    }
   
    // Constructor: make new Splitter with specified max and min values
    public Splitter(int maxCount, int minCount) {
             this.maxCount = maxCount;
        this.minCount = minCount;
    }

    // accessor method: get the current maxLen value
    public int getMaxLen() {
        return maxLen;
    }

    // mutator method: set maxLen to specified value
    public void setMaxLen(int maxLen) {
        this.maxLen = maxLen;
    }


    // the real work gets done here:
    //   -- split the given string into substrings no longer than maxLen
    //   -- return as an array of strings
    public String[] split(String str) {
             int origLen = str.length();      
          
          // calculate the number of substrings we'll need to make
        int splitNum = origLen/maxLen;
        if (origLen % maxLen > 0)
            splitNum += 1;

        // initialize the result array
        String[] splits = new String[splitNum];

        // for each substring...
        for (int i = 0; i < splitNum; i++) {

            // the substring starts here
            int startPos = i * maxLen;

            // the substring ends here
            int endPos = startPos + maxLen;

            // make sure we don't cause an IndexOutOfBoundsException
            if (endPos > origLen)
                endPos = origLen;

            // make the substring
            String substr = str.substring(startPos, endPos);

            // stick it in the result array
            splits[i] = substr;
        }

        // return the result array
        return splits;
    }
   
    public String split(String str, int count) {
             //int origLen = str.length();      
          int splitNum=0;
             
          if (count > maxCount){
                splitNum = count/maxCount;
          }
             
          if (count % maxCount > 0)
                splitNum += 1;
          
          System.out.println("split num inside split method " + splitNum);       
          
          return "test";
    }
   
    public static void main(String[] argv) {
        int splitLen = 5;
        int length,index = 0;
        int count = 0;
       
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String str = "";
       // System.out.println("split length = "+splitLen);
        System.out.print("Enter a string: ");
        try {
                  str = in.readLine();
                  String patternStr = "<br> <br>";
                  
                  Pattern pattern = Pattern.compile(patternStr);
                  Matcher matcher = pattern.matcher(str);
                                              
                  while(matcher.find()){
                     index = str.length();
                     count = count+1;
                     System.out.println("Count in main method is "+ count + " index" + index);
                     String result = new Splitter().split(str,count);
                     System.out.println(" result is "+result);
                  }            
           
        } catch (IOException err) {
            System.out.println("IOException: "+err);
            System.exit(1);
        }
        System.out.println(">>"+str+"<<");  
       
       
    }
}
------------------------------------------------------------------------------------------------------------
I want split the sting based on the regular expression.....  I will do some other calculations later.. But all I need is what could be the best way to cut the string based on that expression.

I'm trying to identify the String index value and will pass that string index to the split method..

Based on the above code can any one help me to find the index number (or) any other way to split it...

ThanksStart Free Trial
[+][-]07.25.2007 at 02:58AM PDT, ID: 19563963

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.25.2007 at 04:42AM PDT, ID: 19564412

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.25.2007 at 06:08AM PDT, ID: 19564968

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Java Server Pages (JSP)
Tags: split, string, based, expression, regular
Sign Up Now!
Solution Provided By: matthew016
Participating Experts: 1
Solution Grade: B
 
 
[+][-]07.26.2007 at 12:24PM PDT, ID: 19577446

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.27.2007 at 12:42AM PDT, ID: 19580738

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32