Revision 46047 trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/java/org/gvsig/expressionevaluator/impl/function/string/InstrFunction.java
InstrFunction.java | ||
---|---|---|
7 | 7 |
public class InstrFunction extends AbstractFunction { |
8 | 8 |
|
9 | 9 |
public InstrFunction() { |
10 |
super("String", "INSTR", Range.between(2, 3)); |
|
10 |
super( |
|
11 |
"String", "INSTR", Range.is(2), |
|
12 |
"The functions search string for substring. The function returns " |
|
13 |
+ "an integer indicating the position of the character " |
|
14 |
+ "in string that is the first character of this occurrence.\n" |
|
15 |
+ "The function returns a numeric value. The first position " |
|
16 |
+ "in the string is 1. If substring is not found in string, " |
|
17 |
+ "then the function will return 0", |
|
18 |
"INSTR"+"({{string}}, substring)", |
|
19 |
new String[]{ |
|
20 |
"string - String in which to perform the search. ", |
|
21 |
"substring - String to search" |
|
22 |
}, |
|
23 |
"Integer", |
|
24 |
true |
|
25 |
); |
|
26 |
// Nota: |
|
27 |
// En SQL Server: CHARINDEX(substring, string) |
|
28 |
// En PostgreSQL: STRPOS(string,substring) o POSITION(substring in string) |
|
29 |
// En Oracle: INSTR(string,substring) |
|
30 |
// En H2: INSTR(string,substring) |
|
11 | 31 |
} |
12 |
|
|
32 |
|
|
13 | 33 |
@Override |
14 | 34 |
public boolean allowConstantFolding() { |
15 | 35 |
return true; |
... | ... | |
19 | 39 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
20 | 40 |
String str = getStr(args, 0); |
21 | 41 |
String search = getStr(args, 1); |
22 |
int start = 0; |
|
23 |
if( args.length == 3 ) { |
|
24 |
start = getInt(args, 2); |
|
42 |
if( str==null || search==null ) { |
|
43 |
return null; |
|
25 | 44 |
} |
26 |
return str.indexOf(search, start); |
|
45 |
int n = str.indexOf(search); |
|
46 |
if( n<0 ) { |
|
47 |
return 0; |
|
48 |
} |
|
49 |
return n+1; |
|
27 | 50 |
} |
28 | 51 |
|
29 | 52 |
|
Also available in: Unified diff