Java Android 使用 indexOf

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20164875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:37:54  来源:igfitidea点击:

Android using indexOf

javaandroidstring

提问by John Jared

I have this, tweet.titleis a string that is equals to I love burgers.

我有这个,tweet.title是一个等于的字符串I love burgers.

        CharSequence i = tweet.title.indexOf(Integer.valueOf("I"));

        SpannableString WordtoSpan = new SpannableString(i);        
        WordtoSpan.setSpan(new ForegroundColorSpan(Color.RED), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        holder.txtTitle.setText(WordtoSpan);

Now I get this error from it Type mismatch: cannot convert from int to CharSequence

现在我从中得到这个错误 Type mismatch: cannot convert from int to CharSequence

I wanna find the Iin the string.

我想I在字符串中找到。

采纳答案by Michael Yaworski

Code (tested and works)

代码(经过测试并有效)

CharSequence i = tweet.title.indexOf(Integer.valueOf("I"));

should be

应该

int index = tweet.title.indexOf("I"); // find int position of "I"

// set to be the String of where "I" is to plus 1 of that position
CharSequence i = tweet.title.substring(index, index + 1);

// Alternative to substring, you could use charAt, which returns a char
CharSequence i = Character.toString(tweet.title.charAt(index));


Explanation

解释

indexOf(String)returns the intpositionof where that Stringis.
You gave Integer.valueOf("I")as the Stringto indexOf(String).

indexOf(String)返回int位置的其中那String是。
你给了Integer.valueOf("I")作为Stringto indexOf(String)

Integer.valueOf(String)converts a Stringto an Integer. Why would you give the indexOf(String)an Integerand why would you try to convert "I"to an Integer?

Integer.valueOf(String)将 a 转换StringInteger。你为什么要给indexOf(String)an Integer,为什么要尝试转换"I"为 an Integer

What you meant to do was this: CharSequence i = tweet.title.indexOf("I");but that is also wrong because it will return an int(position in the String), hence the mismatch error.

你的意思是这样的:CharSequence i = tweet.title.indexOf("I");但这也是错误的,因为它会返回一个int(在 中的位置String),因此会出现不匹配错误。

You need to find the position of "I"in the tweet.title, so that's tweet.title.indexOf("I"). Then set the CharSequenceto be tweet.titleat that position up until that position +1 (so that you get just the one character I).

你需要找到的位置"I"tweet.title,这样的tweet.title.indexOf("I")。然后设置CharSequencetweet.title在该位置,直到该位置+1(这样你得到的只是一个字符I)。

回答by Shayan Pourvatan

String.contains()which checks if the string contains a specified sequence of char values String.indexOf()which returns the index within the string of the first occurence of the specified character or substring (there are 4 variations of this method)

String.contains()它检查字符串是否包含指定的字符值序列,String.indexOf()该序列 返回指定字符或子字符串第一次出现的字符串中的索引(此方法有 4 种变体)

回答by mttdbrd

String.indexOf returns an int not a CharSequence. Also, you can't get the index of the I using Integer.valueOf().

String.indexOf 返回一个 int 而不是 CharSequence。此外,您无法使用 Integer.valueOf() 获取 I 的索引。

    int i = tweet.title.indexOf("I");

If you want to just get the "I" back do this:

如果您只想让“我”回来,请执行以下操作:

    int i = tweet.title.indexOf("I");
    String s = tweet.title.substring(i, i + 1);

Edited: I fixed a couple of bugs in this code.

编辑:我修复了这段代码中的几个错误。

回答by Steve Benett

You can work with String because it implements the CharSequence interface. See the docs.

您可以使用 String ,因为它实现了 CharSequence 接口。请参阅文档

Because you want just one letter you can get the char at the index and make a String out of it:

因为您只需要一个字母,所以您可以在索引处获取字符并从中生成一个字符串:

    int i = tweet.title.indexOf("I");
    String letter = Character.toString(tweet.title.charAt(i));

    SpannableString wordtoSpan = new SpannableString(letter);        
    wordtoSpan.setSpan(
            new ForegroundColorSpan(Color.RED), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

If you wanna get more chars of your String you can use the String.subString(int beginIndex, int endIndex)method.

如果你想获得更多的字符串字符,你可以使用String.subString(int beginIndex, int endIndex)方法。

F.e: you wanna get "burgers":

Fe:你想要“汉堡”:

    int i = tweet.title.indexOf("b");
    String sub = title.subString(i, i + 6);
    SpannableString wordtoSpan = new SpannableString(sub);        
    wordtoSpan.setSpan(new ForegroundColorSpan(Color.RED), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);