我需要将以下php函数转换为android函数

时间:2013-10-28 09:02:09

标签: php android

嗨我需要以下php函数的android函数

function BetweenStr($InputString, $StartStr, $EndStr, $StartLoc=0) {
    if (($StartLoc = strpos($InputString, $StartStr, $StartLoc)) === false) { 
        return; 
    }

    $StartLoc += strlen($StartStr);
    if (!$EndStr) { 
        $EndStr = $StartStr; 
    }
    if (!$EndLoc = strpos($InputString, $EndStr, $StartLoc)) { 
        return;
    }
    return substr($InputString, $StartLoc, ($EndLoc-$StartLoc));
}

echo BetweenStr("Cut1 this will be output cut2", 'cut1', 'cut2', 0)

Result will be :"this will be output

这就是我所做的。但它仍有问题

data="Cut1 this will be output cut2";
String substr=data.subString(data.indexOf("cut1"),data.indexOf("cut2"));

1 个答案:

答案 0 :(得分:0)

public String betweenStr(String input, String open, String close, int startIndex){
    input = input.toLowerCase();
    int start = input.indexOf(open.toLowerCase(), startIndex);
    if(start == -1){
        return null;
    }

    startIndex = start + open.length();
    if(close == null){
        close = open;
    }
    int end = input.indexOf(close.toLowerCase(), startIndex);
    if(end == -1){
        return null;
    }

    return input.substring(startIndex, end);
}

public String betweenStr(String input, String open, String close){
    betweenStr(input, open, close, 0);
}