Skip to Content
0

Replacing special characters with other characters

Apr 07 at 04:47 AM

43

avatar image

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

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

4 Answers

BRM Fresher Apr 09 at 01:00 PM
0

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

Share
10 |10000 characters needed characters left characters exceeded
BRM Fresher Apr 09 at 02:00 PM
0

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

Show 1 Share
10 |10000 characters needed characters left 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

0
Ambrish Mishra Apr 09 at 01:40 PM
0

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

Share
10 |10000 characters needed characters left characters exceeded
Andrzej Filusz Apr 09 at 12:29 PM
0

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

Share
10 |10000 characters needed characters left characters exceeded