-1

I'm not quite sure how to word this so the title is a bit wierd. What I want to do is to colour and change the size of 2 subsituted substrings in a string that is in the format "%1$d/10 %2$s". So say for example the first argument is '1' and the second argument is 'Rubbish' the final string would be "1/10 Rubbish". I want to colour the '1' red and set the style to bold, and colour the 'Rubbish' red, but leave the '/10' unchanged. I know I can do this with Spannables but the problem is I cannot see a way to get the start and end points of the various substrings. I know I can just look for the first '/' and assume that the characters before it are the first format substitution, and then look for the first non-space character after the '10' and know that this is the second format substitution but that seems a bit messy. Surely there should be some way to query the string for the positions of the characters that have been substituted for each format parameter?

user3265561
  • 812
  • 7
  • 22

1 Answers1

0

Not that I know of.

All format methods in Java return either a string or a Formatter(depending on what you're using).

It seems "messy" but not that much, as you said you're probably going to use a variant of indexOf(String str, int fromIndex) twice to get the indexes and then coloring using spannable. So it's probably going to be about 4 to 6 lines of relevant code.

One other way to manually find the indexes is using a for loop to find a '/' and then a ' '. If your string always has this format it will work.

There are regexes as well but it's a bit overkill!

Thiago Valle
  • 449
  • 1
  • 6
  • 20