7

I have created a function wherein I split a string from the csv filecsvAsString.split('\n') . It is working when the string is not so big. But when I try to split a very large string, an error will occur

regex too complicated

what I want to do is to catch if an error occur during the split process. How will I do it ? I've tried to use try and catch but the problem still occur. Is there any other possible solution?

  • Fields can contain embedded line breaks in csv files. – martin Jul 28 '15 at 2:33
  • @martin I didn't understand your purpose. Actually I want to use split because I only need to get the first row. But my main issue here is to trap the possible error occured. – Hope Jul 28 '15 at 2:37
  • That was just a warning that with using split('\n'), you won't get the entire line if one of the fields has a line break in it. – martin Jul 28 '15 at 2:39
12

Not a catch per say (IIRC you cannot catch it, especially if Catch Exception did not work) but will solve the problem of regex to complex, use a custom iterator:

CLASS

public with sharing class Utility_RowIterator implements Iterator<String>, Iterable<String>
{
   private String m_Data;
   private Integer m_index = 0;
   private String m_rowDelimiter = '\n';

   public Utility_RowIterator(String fileData)
   {
      m_Data = fileData; 
   }
   public Utility_RowIterator(String fileData, String rowDelimiter)
   {
      m_Data = fileData; 
      m_rowDelimiter = rowDelimiter;
   }

   public Boolean hasNext()
   {
      return m_index < m_Data.length() ? true : false;
   }
   public String next()
   {     
      Integer key = m_Data.indexOf(m_rowDelimiter, m_index);

      if (key == -1)
        key = m_Data.length();

      String row = m_Data.subString(m_index, key);
      m_index = key + 1;
      return row;
   }
   public Iterator<String> Iterator()
   {
      return this;   
   }
}

CODE

Utility_RowIterator r = New Utility_RowIterator(csv,'\n'); //Replace \n with whatever delineates your row

String firstRow;
if(r.hasNext()) firstRow = r.next();
  • I've tried this , and i think this is the answer of my problem. Thank you... – Hope Jul 28 '15 at 5:29
  • @Hope - Glad it worked for you...... – Eric Jul 28 '15 at 5:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.