Reputation: 800
I have this, tweet.title
is a string that is equals to 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
I wanna find the I
in the string.
Upvotes: 2
Views: 25700
Reputation: 13483
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));
indexOf(String)
returns the int
position of where that String
is.
You gave Integer.valueOf("I")
as the String
to indexOf(String)
.
Integer.valueOf(String)
converts a String
to an Integer
. Why would you give the indexOf(String)
an Integer
and why would you try to convert "I"
to 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.
You need to find the position of "I"
in the tweet.title
, so that's tweet.title.indexOf("I")
. Then set the CharSequence
to be tweet.title
at that position up until that position +1 (so that you get just the one character I
).
Upvotes: 3
Reputation: 12943
You can work with String because it implements the CharSequence interface. See the docs.
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.
F.e: you wanna get "burgers":
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);
Upvotes: 1
Reputation: 1831
String.indexOf returns an int not a CharSequence. Also, you can't get the index of the I using Integer.valueOf().
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.
Upvotes: 4
Reputation: 11948
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)
Upvotes: 3