Statistics
| Revision:

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
package org.gvsig.expressionevaluator.impl.function.string;
2

    
3
import org.apache.commons.lang3.Range;
4
import org.gvsig.expressionevaluator.Interpreter;
5
import org.gvsig.expressionevaluator.spi.AbstractFunction;
6

    
7
public class InstrFunction extends AbstractFunction {
8

    
9
    public InstrFunction() {
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)
31
    }
32
    
33
    @Override
34
    public boolean allowConstantFolding() {
35
        return true;
36
    }
37
    
38
    @Override
39
    public Object call(Interpreter interpreter, Object[] args) throws Exception {
40
        String str = getStr(args, 0);
41
        String search = getStr(args, 1);
42
        if( str==null || search==null ) {
43
            return null;
44
        }
45
        int n = str.indexOf(search);
46
        if( n<0 ) {
47
            return 0;
48
        }
49
        return n+1;
50
    }
51
    
52

    
53
}