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.i
n));
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("IOExce
ption: "+err);
System.exit(1);
}
System.out.println(">>"+st
r+"<<");
}
}
--------------------------
----------
----------
----------
----------
----------
----------
----------
----------
--
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...
Thanks
Start Free Trial