當子字符串出現時,我需要幫助來對字符串進行子字符串。 實施例特定字符串出現時的Java子字符串字符串
初始字符串:123456789abcdefgh
字符串SUBSTR:ABCD
結果:123456789
我檢查SUBSTR方法,但它接受索引位置value.I需要搜索子字符串的出現和傳遞索引?
感謝
當子字符串出現時,我需要幫助來對字符串進行子字符串。 實施例特定字符串出現時的Java子字符串字符串
初始字符串:123456789abcdefgh
字符串SUBSTR:ABCD
結果:123456789
我檢查SUBSTR方法,但它接受索引位置value.I需要搜索子字符串的出現和傳遞索引?
感謝
如果你想字符串從最後一個數字拆分(一),則代碼應該是這樣的:
您可以將字符串
內"a"
更改爲任意字符
package nl.testing.startingpoint;
public class Main {
public static void main(String args[]) {
String[] part = getSplitArray("123456789abcdefgh", "a");
System.out.println(part[0]);
System.out.println(part[1]);
}
public static String[] getSplitArray(String toSplitString, String spltiChar) {
return toSplitString.split("(?<=" + spltiChar + ")");
}
}
記住,從該字符第一次出現toSplitString.split("(?<=" + spltiChar + ")");
分裂。
不,我需要找到像(bcde) –
@AlessioDiMarco之類的其他位置,編輯代碼,只需將char'「a」更改爲要分割的字符串中的任何字符! – Igoranze
希望這可以幫助:
public static void main(final String[] args)
{
searchString("123456789abcdefghabcd", "abcd");
}
public static void searchString(String inputValue, final String searchValue)
{
while (!(inputValue.indexOf(searchValue) < 0))
{
System.out.println(inputValue.substring(0, inputValue.indexOf(searchValue)));
inputValue = inputValue.substring(inputValue.indexOf(searchValue) +
searchValue.length());
}
}
輸出:
123456789
efgh
您需要檢查javadoc的時間長一點。你需要的一切都在那裏......提示:其中一些方法存在多種風味。 – GhostCat
是的,我需要首先用IndexOf方法搜索出現 String = str.substring(0,str.indexOf(substr)); –