简体   繁体   中英

How to replace all special characters with another character in java?

I want to replace all 'special characters' with a special character in java

For example 'cash&carry' will become 'cash+carry' and also 'cash$carry' will become 'cash+carry'

I have a sample CSV file as

Here the CSV headers are 'What' and 'Where'

What,Where
salon,new+york+metro
pizza,los+angeles+metro
crate&barrel,los+angeles+metro
restaurants,los+angeles+metro
gas+station,los+angeles+metro
persian+restaurant,los+angeles+metro
car+wash,los+angeles+metro
book store,los+angeles+metro
garment,los+angeles+metro
"cash,carry",los+angeles+metro
cash&carry,los+angeles+metro
cash carry,los+angeles+metro

The expected output

What,Where
salon,new+york+metro
pizza,los+angeles+metro
crate+barrel,los+angeles+metro
restaurants,los+angeles+metro
gas+station,los+angeles+metro
persian+restaurant,los+angeles+metro
car+wash,los+angeles+metro
book+store,los+angeles+metro
garment,los+angeles+metro
cash+carry,los+angeles+metro
cash+carry,los+angeles+metro
cash+carry,los+angeles+metro

The sample code is as follows

String csvfile="BidAPI.csv"; 

 try{

        // create the 'Array List'
        ArrayList<String> What=new ArrayList<String>();
        ArrayList<String> Where=new ArrayList<String>();

    BufferedReader br=new BufferedReader(new FileReader(csvfile));
        StringTokenizer st=null;
        String line="";
        int linenumber=0;
        int columnnumber;
        int free=0;
        int free1=0;    

        while((line=br.readLine())!=null){
            linenumber++;
            columnnumber=0;

            st=new StringTokenizer(line,",");
            while(st.hasMoreTokens()){
                columnnumber++;
                String token=st.nextToken();
                if("What".equals(token)){
                    free=columnnumber;
                    System.out.println("the value of free  :"+free);
                    } else if("Where".equals(token)){
                    free1=columnnumber;
                    System.out.println("the value of free1 :"+free1);
                    } 

                    if(linenumber>1){

                if (columnnumber==free){
                    What.add(token);    
                } else if(columnnumber==free1){
                    Where.add(token);
                }
            }
        }
    }


    // converting the  'What' Array List to array
    String[] what=What.toArray(new String[What.size()]);

    // converting the 'Where' Array List to array
    String[] where = Where.toArray(new String[Where.size()]);

    for(int i=0;i<what.length;i++){
    String data = what[i].replaceAll("[^A-Za-z0-9\",]| (?!([^\"]*\"){2}[^\"]*$)", "+").replace("\"", "");
    System.out.println(data);
    System.out.println(where[i]);
    String finaldata = data+where[i];
    String json = readUrl(desturl);
    br.close();
    }catch(Exception e){
        System.out.println("There is an error :"+e);
    }   

All the special characters, all the spaces and the double quotes should be removed and replaced as in the desired output.

I am using value.replaceAll("[^A-Za-z0-9 ]", "+") , but it is not working.

Error

 cash
 carry"

Any help is appreciated. new to regex.

You need to:

  • replace all commas within quotes with +
  • replace non-whitelist (and you need to add commas to your whitelist) +
  • remove double quotes

Try this:

line = line.replaceAll("[^A-Za-z0-9\",]|,(?!(([^\"]*\"){2})*[^\"]*$)", "+").replace("\"", "");

I think your regex is pretty close. Add an exception for comma's as well and get rid of the space and you are good.

BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = r.readLine()) != null)
{
    String replaced = line.replace("\"", "");
    replaced = replaced.replaceAll("[^A-Za-z0-9,]", "+");
    System.out.println(replaced);
}

Of course, Strings are immutable in Java. Keep that in mind. replaceAll() returns a new String and does not modify the original instance.

Demo here.

You need to first find quote and replace , inside it with + . Next you can just use replaceAll("[^A-Za-z0-9,]", "+") so you will replace all non alphanumeric characters or , with + . Your code for that can use

Pattern p = Pattern.compile("\"([^\"]*)\"");

pattern to locate quotations and appendReplacement , appendTail from Matcher class to replace founded quotations with its new version.

So in short your code can look something like

Scanner scanner = new Scanner(new File(csvfile));

Pattern p = Pattern.compile("\"([^\"]*)\"");
StringBuffer sb = new StringBuffer();
while(scanner.hasNextLine()){
    String line = scanner.nextLine();
    Matcher m = p.matcher(line);
    while (m.find()){//find quotes
        //and replace their content with content with replaced `,` by `+`
        //BTW group(1) holds part of quotation without `"` marsk
        m.appendReplacement(sb, m.group(1).replace(',', '+'));
    }
    m.appendTail(sb);//we need to also add rest of unmatched data to buffer

    //now we can just normally replace special characters with +
    String result = sb.toString().replaceAll("[^A-Za-z0-9,]", "+");

    //after job is done we can use result, so lest print it
    System.out.println(result);

    //lets not forget to reset buffer for next line
    sb.delete(0, sb.length());
}

Answer to the question

String csvfile="BidAPI.csv"; 

try{

    // create the 'Array List'
    ArrayList<String> What=new ArrayList<String>();
    ArrayList<String> Where=new ArrayList<String>();

  BufferedReader br=new BufferedReader(new FileReader(csvfile));
    StringTokenizer st=null;
    String line="";
    int linenumber=0;
    int columnnumber;
    int free=0;
    int free1=0;    

    while((line=br.readLine())!=null){
         line =line.replaceAll("[^A-Za-z0-9\",]|,(?!(([^\"]*\"){2})*[^\"]*$)", "+").replace("\"", "");
        linenumber++;
        columnnumber=0;

        st=new StringTokenizer(line,",");
        while(st.hasMoreTokens()){
            columnnumber++;
            String token=st.nextToken();
            if("What".equals(token)){
                free=columnnumber;
                System.out.println("the value of free  :"+free);
                } else if("Where".equals(token)){
                free1=columnnumber;
                System.out.println("the value of free1 :"+free1);
                } 

                if(linenumber>1){

            if (columnnumber==free){
                What.add(token);    
            } else if(columnnumber==free1){
                Where.add(token);
            }
        }
    }
}


// converting the  'What' Array List to array
String[] what=What.toArray(new String[What.size()]);

// converting the 'Where' Array List to array
String[] where = Where.toArray(new String[Where.size()]);

for(int i=0;i<what.length;i++){
String data = what[i].replaceAll("[^A-Za-z0-9\",]| (?!([^\"]*\"){2}[^\"]*$)", "+").replace("\"", "");
System.out.println(data);
System.out.println(where[i]);
String finaldata = data+where[i];
String json = readUrl(desturl);
br.close();
}catch(Exception e){
    System.out.println("There is an error :"+e);
}   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM