-4

嗨,我需要以下 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"));
4

1 回答 1

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);
}
于 2013-10-28T09:18:48.937 回答