svn-gvsig-desktop / 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 @ 46047
History | View | Annotate | Download (1.81 KB)
1 | 43512 | jjdelcerro | package org.gvsig.expressionevaluator.impl.function.string; |
---|---|---|---|
2 | |||
3 | import org.apache.commons.lang3.Range; |
||
4 | 43521 | jjdelcerro | import org.gvsig.expressionevaluator.Interpreter; |
5 | 43512 | jjdelcerro | import org.gvsig.expressionevaluator.spi.AbstractFunction; |
6 | |||
7 | public class InstrFunction extends AbstractFunction { |
||
8 | |||
9 | public InstrFunction() {
|
||
10 | 46047 | jjdelcerro | 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)
|
||
31 | 43512 | jjdelcerro | } |
32 | 46047 | jjdelcerro | |
33 | 43512 | jjdelcerro | @Override
|
34 | 44009 | jjdelcerro | public boolean allowConstantFolding() { |
35 | return true; |
||
36 | } |
||
37 | |||
38 | @Override
|
||
39 | 43521 | jjdelcerro | public Object call(Interpreter interpreter, Object[] args) throws Exception { |
40 | 43512 | jjdelcerro | String str = getStr(args, 0); |
41 | String search = getStr(args, 1); |
||
42 | 46047 | jjdelcerro | if( str==null || search==null ) { |
43 | return null; |
||
44 | 43512 | jjdelcerro | } |
45 | 46047 | jjdelcerro | int n = str.indexOf(search);
|
46 | if( n<0 ) { |
||
47 | return 0; |
||
48 | } |
||
49 | return n+1; |
||
50 | 43512 | jjdelcerro | } |
51 | |||
52 | |||
53 | } |