字符串中所有符号的索引

以下代码将扣除 2


String word = "bannanas";
String guess = "n";
int index;
System.out.println/
index = word.indexOf/guess/
/;


我想知道如何获得所有索引 "n" /"guess"/ 排队 "bannanas"

预期结果将如下:
[2,3,5]
已邀请:

冰洋

赞同来自:

这应该显示没有的位置列表
-1

最后

曾是



.


int index = word.indexOf/guess/;
while /index >= 0/ {
System.out.println/index/;
index = word.indexOf/guess, index + 1/;
}


它也可以作为一个循环完成。
for

:


for /int index = word.indexOf/guess/;
index >= 0;
index = word.indexOf/guess, index + 1//
{
System.out.println/index/;
}


[注意:如果
guess

可能长于一个符号,然后可以,分析字符串
guess

, 跑步循环
word

比这更快地实现上述周期。 这种方法的基准是
https://en.wikipedia.org/wiki/ ... rithm
. 但是,有利于使用这种方法的条件似乎不存在。]

二哥

赞同来自:

尝试执行以下内容 /现在不显示该值 -1 在最后!/


int index = word.indexOf/guess/;
while/index >= 0/ {
System.out.println/index/;
index = word.indexOf/guess, index+1/;
}

莫问

赞同来自:

String string = "bannanas";
ArrayList<integer> list = new ArrayList<integer>//;
char character = 'n';
for/int i = 0; i &lt; string.length//; i++/{
if/string.charAt/i/ == character/{
list.add/i/;
}
}


结果将使用如下。 :


for/Integer i : list/{
System.out.println/i/;
}


或作为阵列 :


list.toArray//;


</integer></integer>

八刀丁二

赞同来自:

从 Java9 可以使用
https://docs.oracle.com/javase ... ator-
通过以下方式: -


List<integer> indexes = IntStream
.iterate/word.indexOf/c/, index -&gt; index &gt;= 0, index -&gt; word.indexOf/c, index + 1//
.boxed//
.collect/Collectors.toList///;
System.out.printlnt/indexes/;


</integer>

董宝中

赞同来自:

int index = -1;
while//index = text.indexOf/"on", index + 1// >= 0/ {
LOG.d/"index=" + index/;
}

江南孤鹜

赞同来自:

这可以使用功能方式使用 Java 9 使用正则表达式:


Pattern.compile/Pattern.quote/guess// // sanitize input and create pattern
.matcher/word/ // create matcher
.results// // get the MatchResults, Java 9 method
.map/MatchResult::start/ // get the first index
.collect/Collectors.toList/// // collect found indices into a list
/;


这是解决方案 Kotlin 将此逻辑添加为新方法 a new methods 在
CharSequence

API 使用扩展方法:


// Extension method
fun CharSequence.indicesOf/input: String/: List<int> =
Regex/Pattern.quote/input// // build regex
.findAll/this/ // get the matches
.map { it.range.first } // get the index
.toCollection/mutableListOf/// // collect the result as list

// call the methods as
"Banana".indicesOf/"a"/ // [1, 3, 5]


</int>

二哥

赞同来自:

String word = "bannanas";

String guess = "n";

String temp = word;

while/temp.indexOf/guess/ != -1/ {
int index = temp.indexOf/guess/;
System.out.println/index/;
temp = temp.substring/index + 1/;
}

龙天

赞同来自:

String input = "GATATATGCG";
String substring = "G";
String temp = input;
String indexOF ="";
int tempIntex=1;

while/temp.indexOf/substring/ != -1/
{
int index = temp.indexOf/substring/;
indexOF +=/index+tempIntex/+" ";
tempIntex+=/index+1/;
temp = temp.substring/index + 1/;
}
Log.e/"indexOf ","" + indexOF/;

诸葛浮云

赞同来自:

此外,如果要在行中找到所有行索引。


int index = word.indexOf/guess/;
while /index >= 0/ {
System.out.println/index/;
index = word.indexOf/guess, index + guess.length///;
}

三叔

赞同来自:

我也有这个问题,而我没有提出这种方法。


public static int[] indexesOf/String s, String flag/ {
int flagLen = flag.length//;
String current = s;
int[] res = new int[s.length//];
int count = 0;
int base = 0;
while/current.contains/flag// {
int index = current.indexOf/flag/;
res[count] = index + base;
base += index + flagLen;
current = current.substring/current.indexOf/flag/ + flagLen, current.length///;
++ count;
}
return Arrays.copyOf/res, count/;
}


此方法可用于搜索行中任何长度的任何标志的索引,例如:


public class Main {

public static void main/String[] args/ {
int[] indexes = indexesOf/"Hello, yellow jello", "ll"/;

// Prints [2, 9, 16]
System.out.println/Arrays.toString/indexes//;
}

public static int[] indexesOf/String s, String flag/ {
int flagLen = flag.length//;
String current = s;
int[] res = new int[s.length//];
int count = 0;
int base = 0;
while/current.contains/flag// {
int index = current.indexOf/flag/;
res[count] = index + base;
base += index + flagLen;
current = current.substring/current.indexOf/flag/ + flagLen, current.length///;
++ count;
}
return Arrays.copyOf/res, count/;
}
}

董宝中

赞同来自:

我想出了一个分裂琴弦的课程。 最后得到了短暂的测试。


SplitStringUtils.smartSplitToShorterStrings/String str, int maxLen, int maxParts/

将被空格除以不拆分单词,如果可能,如果没有,它将按照索引除以 maxLen.

提供的其他方法来管理它是如何分割的:
bruteSplitLimit/String str, int maxLen, int maxParts/

,
spaceSplit/String str, int maxLen, int maxParts/

.


public class SplitStringUtils {

public static String[] smartSplitToShorterStrings/String str, int maxLen, int maxParts/ {
if /str.length// <= maxLen/ {
return new String[] {str};
}
if /str.length// > maxLen*maxParts/ {
return bruteSplitLimit/str, maxLen, maxParts/;
}

String[] res = spaceSplit/str, maxLen, maxParts/;
if /res != null/ {
return res;
}

return bruteSplitLimit/str, maxLen, maxParts/;
}

public static String[] bruteSplitLimit/String str, int maxLen, int maxParts/ {
String[] bruteArr = bruteSplit/str, maxLen/;
String[] ret = Arrays.stream/bruteArr/
.limit/maxParts/
.collect/Collectors.toList///
.toArray/new String[maxParts]/;
return ret;
}

public static String[] bruteSplit/String name, int maxLen/ {
List<string> res = new ArrayList&lt;&gt;//;
int start =0;
int end = maxLen;
while /end &lt;= name.length/// {
String substr = name.substring/start, end/;
res.add/substr/;
start = end;
end +=maxLen;
}
String substr = name.substring/start, name.length///;
res.add/substr/;
return res.toArray/new String[res.size//]/;
}

public static String[] spaceSplit/String str, int maxLen, int maxParts/ {
List<integer> spaceIndexes = findSplitPoints/str, ' '/;
List<integer> goodSplitIndexes = new ArrayList&lt;&gt;//;
int goodIndex = -1;
int curPartMax = maxLen;
for /int i=0; i&lt; spaceIndexes.size//; i++/ {
int idx = spaceIndexes.get/i/;
if /idx &lt; curPartMax/ {
goodIndex = idx;
} else {
goodSplitIndexes.add/goodIndex+1/;
curPartMax = goodIndex+1+maxLen;
}
}
if /goodSplitIndexes.get/goodSplitIndexes.size//-1/ != str.length/// {
goodSplitIndexes.add/str.length///;
}
if /goodSplitIndexes.size//&lt;=maxParts/ {
List<string> res = new ArrayList&lt;&gt;//;
int start = 0;
for /int i=0; i<goodsplitindexes.size ;="" end="goodSplitIndexes.get/i/;" end-start="" i++="" if="" int="" {=""> maxLen/ {
return null;
}
res.add/str.substring/start, end//;
start = end;
}
return res.toArray/new String[res.size//]/;
}
return null;
}


private static List<integer> findSplitPoints/String str, char c/ {
List<integer> list = new ArrayList<integer>//;
for /int i = 0; i &lt; str.length//; i++/ {
if /str.charAt/i/ == c/ {
list.add/i/;
}
}
list.add/str.length///;
return list;
}
}


简单的测试代码:


public static void main/String[] args/ {
String [] testStrings = {
"123",
"123 123 123 1123 123 123 123 123 123 123",
"123 54123 5123 513 54w567 3567 e56 73w45 63 567356 735687 4678 4678 u4678 u4678 56rt64w5 6546345",
"1345678934576235784620957029356723578946",
"12764444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444",
"3463356 35673567567 3567 35 3567 35 675 653 673567 777777777777777777777777777777777777777777777777777777777777777777"
};

int max = 35;
int maxparts = 2;


for /String str : testStrings/ {
System.out.println/"TEST\n |"+str+"|"/;
printSplitDetails/max, maxparts/;
String[] res = smartSplitToShorterStrings/str, max, maxparts/;
for /int i=0; i&lt; res.length;i++/ {
System.out.println/" "+i+": "+res[i]/;
}
System.out.println/"==========================================================================================================================================================="/;
}

}

static void printSplitDetails/int max, int maxparts/ {
System.out.print/" X: "/;
for /int i=0; i<max*maxparts; "-"="" "|"="" ;="" <="" code]="" div="" else="" i%max="0/" i++="" if="" system.out.print="" system.out.println="" {="" }="" }[="">
<div class="answer_text">
这是一个解决方案 java 8.


[code]public int[] solution /String s, String subString/{
int initialIndex = s.indexOf/subString/;
List<integer> indexList = new ArrayList&lt;&gt;//;
while /initialIndex &gt;=0/{
indexList.add/initialIndex/;
initialIndex = s.indexOf/subString, initialIndex+1/;
}
int [] intA = indexList.stream//.mapToInt/i-&gt;i/.toArray//;
return intA;
}


</integer></div>
<div class="answer_text">
这可以通过迭代来完成
myString

和Shift参数
fromIndex


indexOf//

:


int currentIndex = 0;

while /
myString.indexOf/
mySubstring,
currentIndex/ &gt;= 0/ {

System.out.println/currentIndex/;

currentIndex++;
}


</div>
<div class="answer_text">
试试吧


String str = "helloslkhellodjladfjhello";
String findStr = "hello";

System.out.println/StringUtils.countMatches/str, findStr//;


</div>
</max*maxparts;></integer></integer></integer></goodsplitindexes.size></string></integer></integer></string>

石油百科

赞同来自:

这是一个解决方案 java 8.


public int[] solution /String s, String subString/{
int initialIndex = s.indexOf/subString/;
List<integer> indexList = new ArrayList&lt;&gt;//;
while /initialIndex &gt;=0/{
indexList.add/initialIndex/;
initialIndex = s.indexOf/subString, initialIndex+1/;
}
int [] intA = indexList.stream//.mapToInt/i-&gt;i/.toArray//;
return intA;
}


</integer>

二哥

赞同来自:

这可以通过迭代来完成
myString

和Shift参数
fromIndex


indexOf//

:


int currentIndex = 0;

while /
myString.indexOf/
mySubstring,
currentIndex/ >= 0/ {

System.out.println/currentIndex/;

currentIndex++;
}

诸葛浮云

赞同来自:

试试吧


String str = "helloslkhellodjladfjhello";
String findStr = "hello";

System.out.println/StringUtils.countMatches/str, findStr//;

要回复问题请先登录注册