Skip to Content

Replacing special characters with other characters

Hello Experts,

I need to write a UDF which will replace special characters with other characters like his

@ with a

# with H

$ with S

% with L

and so on.

but while writing the UDF the problem is that replaceALL method replaces whatevar characters with only one value while in my case I need every value to be replaced with a specific value. Is there any work around on this?

Thanks in advance

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • Apr 09 at 01:00 PM

    Thanks Andrzej,

    How can I use this code in UDF in mapping?

    This is how I am using it in UDF in mapping but giving the error

    Execution Type - all value of a queue

    Category - User Defined

    Argument var1

    Result result

    and using this code like this in UDF

    if (var1 == null || var1.length() == 0) return var1; var1 = var1.replaceAll("@", "a"); var1 = var1.replaceAll("#", "H"); var1 = var1.replaceAll("\\$", "S"); var1 = var1.replaceAll("%", "L"); return var1;

    But giving error

    Can you please help me how to use it in UDF?

    Thanks in advance

    Add comment
    10|10000 characters needed characters exceeded

  • Apr 09 at 02:00 PM

    Hi Ambrish,

    I am using this code in UDF

    if (var1 == null || var1.length() == 0) return var1; var1 = var1.replaceAll("@", "a"); var1 = var1.replaceAll("#", "H"); var1 = var1.replaceAll("\\$", "S"); var1 = var1.replaceAll("%", "L"); return var1;

    But getting syntax error,

    execution type: is single values

    Can you please help on this on how to use it in UDF?

    Thanks in advance

    Add comment
    10|10000 characters needed characters exceeded

    • Hi,

      Please check the code below:

      public String myReplace(String var1, Container container) throws StreamTransformationException{
      	if (var1 == null || var1.length() == 0)
      		return var1;
      
      	var1 = var1.replaceAll("@", "a");
      	var1 = var1.replaceAll("#", "H");
      	var1 = var1.replaceAll("\\$", "S");
      	var1 = var1.replaceAll("%", "L");
      	return var1;
      }

      Best Regards,

      Andrzej Filusz

  • Apr 09 at 01:40 PM

    What is the error you are getting. It should work!

    Add comment
    10|10000 characters needed characters exceeded

  • Apr 09 at 12:29 PM

    Hi,

    I'm not sure I understand your problem. Please check the code below and let me know if it solves your issue.

    public class Replace_T {
    	
    	public static String myReplace(String var1) {
    		
    		if (var1 == null || var1.length() == 0)
    			return var1;
    
    
    		var1 = var1.replaceAll("@", "a");
    		var1 = var1.replaceAll("#", "H");
    		var1 = var1.replaceAll("\\$", "S");
    		var1 = var1.replaceAll("%", "L");
    		return var1;
    	}
    	
    	public static void main(String[] args) {
    		System.out.println(myReplace("@#$%@#$%@#$%@#$%"));
    	}	
    }
    

    Best Regards,

    Andrzej Filusz

    Add comment
    10|10000 characters needed characters exceeded