This discussion is archived
13 Replies Latest reply: Mar 5, 2008 5:02 PM by 807591 RSS

Creating a Substring out of String seperated by comma

807591 Newbie
Currently Being Moderated
Hi,

My requirement is that i have a String with comma seperated value.

Say String str=''1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20...............";

In my SQL query I need the following.

OR(str1) OR(str2);

Where str1 is the first 999 strings from the String str.

i.e str1="1,2,3,4................999"
and str2="1000,1001.......................1999"

How can i do this in java.Can anyone please help me out??

Regards,
priya
  • 1. Re: Creating a Substring out of String seperated by comma
    807591 Newbie
    Currently Being Moderated
    Read the String API and look at split(String, int).
  • 2. Re: Creating a Substring out of String seperated by comma
    807591 Newbie
    Currently Being Moderated
    I suppose to could use split, but it sounds like you just need to divide the string in two, on the 999th comma.
    Loop through the string counting commas then use substring. Problem, solved.
  • 3. Re: Creating a Substring out of String seperated by comma
    807591 Newbie
    Currently Being Moderated
    The thing is that the split funtion will retrun me the string alone in an array without the comma.

    Say my Query is a.rule IN (str1 OR str2).

    Where my str1=''1,2,3,4...999"; and str2="1000,1001.........1999";

    But if i use split then the returned array is {1,2,3,4,5,6.............2000} how will i use it in my OR statement.
  • 4. Re: Creating a Substring out of String seperated by comma
    807591 Newbie
    Currently Being Moderated
    The value is not constant.

    Say i might end up having only 998 items then what will happen?

    And even i might end up having 4977. Then in that case what will be the solution.
  • 5. Re: Creating a Substring out of String seperated by comma
    807591 Newbie
    Currently Being Moderated
    I guess what you are looking for is this (It's a very crude method, and personally I don't like it, but that's all I could come up with quickly):
    import java.util.*;
    import java.awt.*;
    import java.io.*;
    
    public class Test
    {
      public static void main (String args []) throws Exception
      {
         String s = "1,2,3,4,5,6,7,8,9,10,11,12"; // Assuming they are contiguous numbers and are separated by single coma with no space
         int breakIdx = 6;  // This can be set to anything, in your case, it will be 999
         
         String a [] = s.split (",");
         int len = a.length; // How many numbers are there
         
         System.out.println ("Total values = " + len);
         
         int startIdx = 0;
         ArrayList list = new ArrayList ();
         int cou = 0;
         for (int i=breakIdx;i < len;i+=breakIdx)
         {
              cou++;
              int endIdx = -1;
              String substrOn = "";
              if (i == 1)
                  substrOn = i + ",";
              else if (i == len)
                  substrOn = "," + i;
              else
                  substrOn = "," + i + ",";
              
              endIdx = s.indexOf (substrOn);
    
              endIdx += substrOn.length () - 1;
              //System.out.println ("\n" + i + ": substrOn==" + substrOn + "  start==" + startIdx + "  end==" + endIdx);
              String temp = s.substring (startIdx, endIdx);
              System.out.println (cou + "==" + temp);
              list.add (temp);
              startIdx = endIdx + 1;
         }
         
         if (startIdx < s.length ())
         {
           String temp = s.substring (startIdx);
           System.out.println (++cou + "==" + temp);
           list.add (temp);
         }
      }
    }
    Here's the result for different breakIdx values:
    breakIdx = 6
    
    Total values = 12
    1==1,2,3,4,5,6
    2==7,8,9,10,11,12
    
    breakIdx = 1
    
    Total values = 12
    1==1
    2==2
    3==3
    4==4
    5==5
    6==6
    7==7
    8==8
    9==9
    10==10
    11==11
    12==12
    
    breakIdx = 2
    
    Total values = 12
    1==1,2
    2==3,4
    3==5,6
    4==7,8
    5==9,10
    6==11,12
  • 6. Re: Creating a Substring out of String seperated by comma
    807591 Newbie
    Currently Being Moderated
    Oops this was wrong.

    Edited by: Eric_C on Mar 5, 2008 5:02 PM
  • 7. Re: Creating a Substring out of String seperated by comma
    807591 Newbie
    Currently Being Moderated
    public class Test
    {
      public static void main(String... args) throws Exception
      {
        String str = "1,2,3,997,998,999,1000,1001,1002,1998,1999,2000,2001,2002";
        String[] parts = str.split("(?<=([,\\d])\\d{3}),(?=(?!\\1)\\d{4})");
        for (String s : parts)
        {
          System.out.println(s);
        }
      }
    }
    output:
    1,2,3,997,998,999
    1000,1001,1002,1998,1999
    2000,2001,2002
  • 8. Re: Creating a Substring out of String seperated by comma
    807591 Newbie
    Currently Being Moderated
    sunpriya wrote:
    The thing is that the split funtion will retrun me the string alone in an array without the comma.

    Say my Query is a.rule IN (str1 OR str2).

    Where my str1=''1,2,3,4...999"; and str2="1000,1001.........1999";

    But if i use split then the returned array is {1,2,3,4,5,6.............2000} how will i use it in my OR statement.
    oh ok. I misunderstood before. How about this:
    {code}
    public String count(){
         String strSplit1 = "";
         
         String str1;
         String str2;
         
         int index = myStr.indexOf( "999" );
         if( index > 0 ){
              str1 = myStr.substring( 0 , index );
              str2 = myStr.substring( index, myStr.length() );
         }
         else{
              str1 = myStr;
         }
         
         
         return strSplit1;
    }
    {code}
  • 9. Re: Creating a Substring out of String seperated by comma
    807591 Newbie
    Currently Being Moderated
    Thanks all for your suggestions. But the solutions provided didnt help me out might be because i didnt clearly explain my question in the forum i think.

    I have an issue in oracle that if the IN clause contains more than 1000 expressions within it then it would throw an exception stating that Maximum allowed expressions is only 1000.

    The input to my inclause is a string which contains more than 1000 values it can be anything more than thousand.

    Say

    String str = "800050280,800051971,800050277,800050960,800050682,800050169,800050448,800051708,800050333,800051195...........................".

    So now what i need to do is i have to split the above mentioned String such that it will be splitted into many substrings each with values less than 1000 strings.

    str1="800050280,800051971,800050277,......." - upto 999.
    str2="800050831,800051926,800051672,......." - upto 999.

    Thus in my oracle query i will try using in the first string , then second string etc...So that i can overcome the oracle limitation. i.e

    IN(str1) OR (str2) OR(str3) etc...

    Hope i am clear this time.. So how can i split the String like i mentioned above in JAVA.Can anyone help me out?
  • 10. Re: Creating a Substring out of String seperated by comma
    807591 Newbie
    Currently Being Moderated
    sunpriya wrote:

    String str = "800050280,800051971,800050277,800050960,800050682,800050169,800050448,800051708,800050333,800051195...........................".

    So now what i need to do is i have to split the above mentioned String such that it will be splitted into many substrings each with values less than 1000 strings.

    str1="800050280,800051971,800050277,......." - upto 999.
    str2="800050831,800051926,800051672,......." - upto 999.
    do you consider 800050280,800051971,800050277 3 strings?


    if so, why isn't uncle_alice's response your answer?

    Edited by: JacobsB on Mar 6, 2008 4:55 PM
  • 11. Re: Creating a Substring out of String seperated by comma
    807591 Newbie
    Currently Being Moderated
    Try this. It's a slight modification to my earlier program:
    import java.util.*;
    
    public class Test
    {
      public static void main (String args []) throws Exception
      {
         String s = "1,2,3,111,344,1123,9934,999,456"; // Assuming they are are separated by single coma with no space
         int breakIdx = 5;  // This is the count after which you want to break the string. This can be set to anything, in your case, it will be 999
         
         String a [] = s.split (",");
         int len = a.length; // How many numbers are there
         
         System.out.println ("Total values = " + len);
         
         int startIdx = 0;
         ArrayList list = new ArrayList ();
         int cou = 0;
         for (int i=breakIdx;i < len;i+=breakIdx)
         {
              cou++;
              int endIdx = -1;
              String substrOn = "";
              if (i == 1)
                  substrOn = a[i-1] + ",";
              else if (i == len)
                  substrOn = "," + a[i-1];
              else
                  substrOn = "," + a[i-1] + ",";
              
              endIdx = s.indexOf (substrOn);
     
              endIdx += substrOn.length () - 1;
              //System.out.println ("\n" + i + ": substrOn==" + substrOn + "  start==" + startIdx + "  end==" + endIdx);
              String temp = s.substring (startIdx, endIdx);
              System.out.println (cou + "==" + temp);
              list.add (temp);
              startIdx = endIdx + 1;
         }
         
         if (startIdx < s.length ())
         {
           String temp = s.substring (startIdx);
           System.out.println (++cou + "==" + temp);
           list.add (temp);
         }
      }
    }
  • 12. Re: Creating a Substring out of String seperated by comma
    807591 Newbie
    Currently Being Moderated
    My earlier post was not really intended as a solution, but as way to elicit a more specific description of the problem (and--I admit it!--as a way to have fun with regexes). Now we know the string needs to be split at every thousandth delimiter, not at every token that represents a multiple of a thousand. amit_chauhan's solutions will do that, but they do a lot of unnecessary work in the process. I would just count the commas in the original string and take a substring at every thousandth one.
  • 13. Re: Creating a Substring out of String seperated by comma
    807591 Newbie
    Currently Being Moderated
    uncle_alice wrote:
    My earlier post was not really intended as a solution, but as way to elicit a more specific description of the problem (and--I admit it!--as a way to have fun with regexes).
    yeah, that was quite impressive. i admit i tried to decipher it, but learned i need a lot more training and practice with regexes.