我如何找出一个字符的位置 [英] How do I find out the position of a char

查看:106
本文介绍了我如何找出一个字符的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个String("Dinosaur"),但我不完全知道该怎么做,但是我如何获得char"o"的位置,并且是否有可能像我的String那样获得两个位置是(泳池")

I've got an String ("Dinosaur") and I don't exactly know how, but how do I get the position of the char "o" and is it in all possible to get two positions like if my String was ("Pool")

推荐答案

对于第一个问题,您可以使用

As for your first question, you can use String#indexOf(int) to get the index of every 'o' in your string.

int oPos = yourString.indexOf('o');

对于第二个问题,可以通过使用

As for your second question, it is possible to get all positions of a given char by making a method which uses String.indexOf(int, int), tracking the previous index so that you don't repeat searched portions of the string. You could store the positions in an array or list.

这篇关于我如何找出一个字符的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆