cancel
Showing results for 
Search instead for 
Did you mean: 

Check for Particular value in the String

Former Member
0 Kudos

XI Experts,

It is a easy question but I don't the answer:-)

How to use "Contains" in the UDF?

I need to find out particular string in the long string, it can be at any position of the string..for e.g

I need to find "TEST" in the given string, how can I do this in UDF?

Marks will be given:-)

Thank you

MP

Accepted Solutions (0)

Answers (4)

Answers (4)

rodrigoalejandro_pertierr
Active Contributor
0 Kudos

the other option could be the standar function indexOf which return the position where the second string if found for the first time in the fisrt string

"Hello World"---->

-


> IndexOf-----> Return 6

"World------->

Take in mind that function is key sensitive so if you look for "world" the result will be "-1"

Former Member
0 Kudos

Thank you everyone for your helful tips..

My problem is resolved:-)

MP

Former Member
0 Kudos

Please close this thread. Thanks.

Former Member
0 Kudos

Better solution is to use indexOf(). If the string is not present, it will return -1.

Example:-

String str1 = "This is a string";

String str2 = "string";

int result_index = str1.indexOf(str2);

if (result_index != -1)

{

your logic

}

Former Member
0 Kudos

There is standard function that take two strings, retun index number if the second one contains first one. otherwise return -1

The result is similar like java function IndexOf();

If you really need to code UDF, you can also code like a.IndexOf("b") to check if b inside a, or use contains like previous reply

Regards

Liang

Edited by: Liang Ji on Sep 23, 2010 10:13 PM

rodrigoalejandro_pertierr
Active Contributor
0 Kudos

see this clear example.

public class StringContainsExample {

String string="java string contains character";

boolean checkContains1=string.contains("ja");

boolean checkContains2=string.contains("xa");

System.out.println("check contains string 1 :"+checkContains1);

System.out.println("check contains string 2 :"+checkContains2);

}

Output

check contain string 1 :true

check contain string 2 :false