function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vasani ParthVasani Parth 

How to remove special characters in CSV file

 I have writen a apex class to import csv file from the VF page . Now I want to remove special characters(@,#.% etc) from particular column of CSV file say "Account Name" . How would do I that with my below code ? Thanks.
Would be glad if anyone can help finding a way in my code.
public class importDataFromCSVController
 {
    public Blob csvFileBody{get;set;}
    public string csvAsString{get;set;}
    public String[] csvFileLines{get;set;}
    public List<account> acclist{get;set;}
    
    public importDataFromCSVController()
    {
        csvFileLines = new String[]{};
        acclist = New List<Account>(); 
    }
  
 public void importCSVFile()
 {
       try
       {
           csvAsString = csvFileBody.toString();
           csvFileLines = csvAsString.split('\n'); 
            
           for(Integer i=1;i<csvFileLines.size();i++)
           {
               Account accObj = new Account() ;
               string[] csvRecordData = csvFileLines[i].split(',');
               accObj.name = csvRecordData[0] ;             
               accObj.accountnumber = csvRecordData[1];
               accObj.Type = csvRecordData[2];
               accObj.AccountSource = csvRecordData[3];   
               accObj.Industry = csvRecordData[4];                                                                             
               acclist.add(accObj);   
           }
            //insert acclist;
        }
        catch (Exception e)
        {
            ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured while importin data Please make sure input csv file is correct');
            ApexPages.addMessage(errorMessage);
        }  
  }
}
<apex:page controller="importDataFromCSVController">
    <apex:form >
        <apex:pagemessages />
        <apex:pageBlock >
            <apex:pageBlockSection columns="4"> 
                  <apex:inputFile value="{!csvFileBody}"  filename="{!csvAsString}"/>
                  <apex:commandButton value="Import Account" action="{!importCSVFile}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
        <apex:pageBlock >
           <apex:pageblocktable value="{!accList}" var="acc">
              <apex:column value="{!acc.name}" />
              <apex:column value="{!acc.AccountNumber}" />
              <apex:column value="{!acc.Type}" />
              <apex:column value="{!acc.Accountsource}" />
              <apex:column value="{!acc.Industry }" />
        </apex:pageblocktable>
     </apex:pageBlock>
   </apex:form>
</apex:page>
Any help will be appreciated .
 
Navee RahulNavee Rahul
Hello Parath ,

you can use the below to get work 

 
String replaceToken = '[^\w\s'](?=(([^']*'){2})*[^']*$)';
		
		for(Integer i=1;i<csvFileLines.size();i++)
           {
               Account accObj = new Account() ;
               string[] csvRecordData = csvFileLines[i].split(',');
			   System.debug('REPLACEMENT ' + csvRecordData[0].replaceAll(replaceToken, ''));
               accObj.name = csvRecordData[0].replaceAll(replaceToken, '') ;             
               accObj.accountnumber = csvRecordData[1];
               accObj.Type = csvRecordData[2];
               accObj.AccountSource = csvRecordData[3];   
               accObj.Industry = csvRecordData[4];                                                                             
               acclist.add(accObj);   
           }

check and let me know.

Thanks
D Naveen Rahul.
Vasani ParthVasani Parth
Hi Naveen,

Thanks for the help .I'm getting an error which is below
Error: Compile Error: Invalid string literal '[^\w\s'. Illegal character sequence '\w' in string literal. at line 21 column 32
I tried with \\w but it still not working .Plese help