cancel
Showing results for 
Search instead for 
Did you mean: 

Help with UDF

Former Member
0 Kudos

Dear All,

I am have the following requirement, which I am trying to put in the UDF and facing some challenges. Kindly help.

In the UDF, I have a constant, say CONST = "A".

There is a variable, say VAR. I have to count the number of occurrences for "A" in VAR.

I am using the below code, which is throwing error (it's just a piece from rest of the code):-


char CONST = 'A';
int count = 0;
int VAR_Length = VAR.length();

	for (int i = 0; i < VAR_Length; i++)
	{
					if (CONST.equals(VAR<i>))
								Count++;
	}

I am using "Single Values" rather than "Queue" in the UDF. I intend to return a flag value of "x" from my UDF, so I am not sure how I can use "Queues" in here.

Please help. Thanks for your help and efforts.

--Abhi

Accepted Solutions (1)

Accepted Solutions (1)

justin_santhanam
Active Contributor
0 Kudos

Abhi,

VAR is single variable from the source or it's an Array?

For example let say VAR = "CAN YOU FIND A IN THIS SENTENCE" ; - Do you want to find how many A's in that VAR?

raj.

Former Member
0 Kudos

Thanks for your reply Raj. Yes, your example is correct, that is my requirement. Kindly assist.

--Abhi

justin_santhanam
Active Contributor
0 Kudos

Abhi,

There will be 1000 ways of doing it. I'm just proposing only one solution

create a simple UDF, with one parameter as input (VAR)


	String find ="A";
		
		int cnt =0;
		for(int i=0;i<VAR.length();i++)
		{
			char c = VAR.charAt(i);
			if(find.indexOf(c)!=-1)
			{
				cnt+=1;
			}
		}
		
		return ""+cnt; 

I don't want to take credit saying that I wrote the above code - I just googled it and found it in - http://home.cogeco.ca/~ve3ll/jatutor7.htm

Hope it helps!

raj.

Former Member
0 Kudos

Hi Raj,

Thanks for your reply.

Actually, I got another way out for the same ...

String s = "Count, the number,, of commas.";

int numberOfCommas = s.replaceAll("[^,]","").length();

I will be using the above code. Anyways, I appreciate your kind help and time. Will still be rewarding you the full points.

Thanks,

--Abhi

Answers (0)