How do I get the position of a substring that was formatted by getString()?
up vote
2
down vote
favorite
How can I get the position of the substring that was read in by
getString(int resId, Object... formatArgs)?
For example I've got these resources
<string name="fruit">pear</string>
<string name="sentence">I like a <xliff:g id="fruit" example="apple">%s</xliff:g>.</string>
and read them in by
String fruit = getString(R.string.fruit); // = "pear"
String sentence = getString(R.string.sentence, fruit); // = "I like a pear."
I'd like to know where the word "pear" starts for example for underlining it. When the mentioned fruit is at the end of the sentence I can go for
int pos = sentence.length() - fruit.length() - 1;
but this breaks as soon as this changes (for example in a different language). I could search for fruit in sentence but this might break as soon as the same fruit gets mentioned twice for example.
Any ideas about how I can do this reliable? Sadly changing the implementation of resources and data parsing is not an option.
java
add a comment |
up vote
2
down vote
favorite
How can I get the position of the substring that was read in by
getString(int resId, Object... formatArgs)?
For example I've got these resources
<string name="fruit">pear</string>
<string name="sentence">I like a <xliff:g id="fruit" example="apple">%s</xliff:g>.</string>
and read them in by
String fruit = getString(R.string.fruit); // = "pear"
String sentence = getString(R.string.sentence, fruit); // = "I like a pear."
I'd like to know where the word "pear" starts for example for underlining it. When the mentioned fruit is at the end of the sentence I can go for
int pos = sentence.length() - fruit.length() - 1;
but this breaks as soon as this changes (for example in a different language). I could search for fruit in sentence but this might break as soon as the same fruit gets mentioned twice for example.
Any ideas about how I can do this reliable? Sadly changing the implementation of resources and data parsing is not an option.
java
"Sadly changing the implementation of resources and data parsing is not an option" -- does this mean you can't change the string resources at all? Can you change thexlifftag even?
– Ben P.
Nov 9 at 22:18
I noticed a bug of a software in my native language and that it's present in other languages as well (actually it's implemented withint pos = sentence.length() - fruit.length() - 1;right now). I want to fix this via code because otherwise every file for every language has to be modified. As thexlifftag is in the resources I'd say it's set in stone as well.
– J. Doe
Nov 9 at 22:27
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
How can I get the position of the substring that was read in by
getString(int resId, Object... formatArgs)?
For example I've got these resources
<string name="fruit">pear</string>
<string name="sentence">I like a <xliff:g id="fruit" example="apple">%s</xliff:g>.</string>
and read them in by
String fruit = getString(R.string.fruit); // = "pear"
String sentence = getString(R.string.sentence, fruit); // = "I like a pear."
I'd like to know where the word "pear" starts for example for underlining it. When the mentioned fruit is at the end of the sentence I can go for
int pos = sentence.length() - fruit.length() - 1;
but this breaks as soon as this changes (for example in a different language). I could search for fruit in sentence but this might break as soon as the same fruit gets mentioned twice for example.
Any ideas about how I can do this reliable? Sadly changing the implementation of resources and data parsing is not an option.
java
How can I get the position of the substring that was read in by
getString(int resId, Object... formatArgs)?
For example I've got these resources
<string name="fruit">pear</string>
<string name="sentence">I like a <xliff:g id="fruit" example="apple">%s</xliff:g>.</string>
and read them in by
String fruit = getString(R.string.fruit); // = "pear"
String sentence = getString(R.string.sentence, fruit); // = "I like a pear."
I'd like to know where the word "pear" starts for example for underlining it. When the mentioned fruit is at the end of the sentence I can go for
int pos = sentence.length() - fruit.length() - 1;
but this breaks as soon as this changes (for example in a different language). I could search for fruit in sentence but this might break as soon as the same fruit gets mentioned twice for example.
Any ideas about how I can do this reliable? Sadly changing the implementation of resources and data parsing is not an option.
java
java
asked Nov 9 at 22:14
J. Doe
184
184
"Sadly changing the implementation of resources and data parsing is not an option" -- does this mean you can't change the string resources at all? Can you change thexlifftag even?
– Ben P.
Nov 9 at 22:18
I noticed a bug of a software in my native language and that it's present in other languages as well (actually it's implemented withint pos = sentence.length() - fruit.length() - 1;right now). I want to fix this via code because otherwise every file for every language has to be modified. As thexlifftag is in the resources I'd say it's set in stone as well.
– J. Doe
Nov 9 at 22:27
add a comment |
"Sadly changing the implementation of resources and data parsing is not an option" -- does this mean you can't change the string resources at all? Can you change thexlifftag even?
– Ben P.
Nov 9 at 22:18
I noticed a bug of a software in my native language and that it's present in other languages as well (actually it's implemented withint pos = sentence.length() - fruit.length() - 1;right now). I want to fix this via code because otherwise every file for every language has to be modified. As thexlifftag is in the resources I'd say it's set in stone as well.
– J. Doe
Nov 9 at 22:27
"Sadly changing the implementation of resources and data parsing is not an option" -- does this mean you can't change the string resources at all? Can you change the
xliff tag even?– Ben P.
Nov 9 at 22:18
"Sadly changing the implementation of resources and data parsing is not an option" -- does this mean you can't change the string resources at all? Can you change the
xliff tag even?– Ben P.
Nov 9 at 22:18
I noticed a bug of a software in my native language and that it's present in other languages as well (actually it's implemented with
int pos = sentence.length() - fruit.length() - 1; right now). I want to fix this via code because otherwise every file for every language has to be modified. As the xliff tag is in the resources I'd say it's set in stone as well.– J. Doe
Nov 9 at 22:27
I noticed a bug of a software in my native language and that it's present in other languages as well (actually it's implemented with
int pos = sentence.length() - fruit.length() - 1; right now). I want to fix this via code because otherwise every file for every language has to be modified. As the xliff tag is in the resources I'd say it's set in stone as well.– J. Doe
Nov 9 at 22:27
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
My recommendation would be to change your string resources to use the <annotation> tag instead of the <xliff:g> tag:
<string name="sentence">I like a <annotation id="fruit" example="apple">%s</annotation>.</string>
If you can do that, then you can retrieve the string resource as a SpannedString using getResources().getText() and then inspect the spans:
SpannedString spanned = (SpannedString) getResources().getText(R.string.sentence);
Annotation spans = spanned.getSpans(0, spanned.length(), Annotation.class);
int startPosition = spanned.getSpanStart(spans[0]);
Then you can format it for display:
String fruit = getString(R.string.fruit);
String sentence = String.format(spanned.toString(), fruit);
If you can't change your resources at all, you can do something similar by using getString() without extra parameters to retrieve the string with its format arguments (%s) and then use indexOf() on that:
String text = getResources().getString(R.string.sentence);
int startPosition = text.indexOf("%s");
And then format it for display the same way:
String fruit = getString(R.string.fruit);
String sentence = String.format(text, fruit);
This is worse for many reasons (the string could theoretically include "%s" as a non-format-argument substring, you need to know the exact specification of the format arguments ahead of time, etc). But it might be the best you can do if you can't change anything else.
add a comment |
up vote
0
down vote
You can split Your string like this:
String parts = sentence.split(" " + fruit + " ");
String part1 = parts[0];
int pos = part1.length() + 1;
part1 is a string that contains everything before " pear ", so its length is the position where word "pear" starts minus 1 (we need to use " " before and after the word to ensure that we will not find it as part of unrelated word in sentence).
That's some nice idea but this breaks for sentences like "I like pears as pears are my favorite fruit." (part1 = "I like",part2 = "as pears are my favorite fruit."- assuming it splits at first opportunity). So I wouldn't deem this as reliable.
– J. Doe
Nov 9 at 22:33
add a comment |
up vote
0
down vote
Use the indexOf method
int pos = sentence.indexOf(fruit);
copy-paste: That's some nice idea but this breaks for sentences like "I like pears as pears are my favorite fruit.". So I wouldn't deem this as reliable.
– J. Doe
Nov 9 at 22:36
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
My recommendation would be to change your string resources to use the <annotation> tag instead of the <xliff:g> tag:
<string name="sentence">I like a <annotation id="fruit" example="apple">%s</annotation>.</string>
If you can do that, then you can retrieve the string resource as a SpannedString using getResources().getText() and then inspect the spans:
SpannedString spanned = (SpannedString) getResources().getText(R.string.sentence);
Annotation spans = spanned.getSpans(0, spanned.length(), Annotation.class);
int startPosition = spanned.getSpanStart(spans[0]);
Then you can format it for display:
String fruit = getString(R.string.fruit);
String sentence = String.format(spanned.toString(), fruit);
If you can't change your resources at all, you can do something similar by using getString() without extra parameters to retrieve the string with its format arguments (%s) and then use indexOf() on that:
String text = getResources().getString(R.string.sentence);
int startPosition = text.indexOf("%s");
And then format it for display the same way:
String fruit = getString(R.string.fruit);
String sentence = String.format(text, fruit);
This is worse for many reasons (the string could theoretically include "%s" as a non-format-argument substring, you need to know the exact specification of the format arguments ahead of time, etc). But it might be the best you can do if you can't change anything else.
add a comment |
up vote
2
down vote
accepted
My recommendation would be to change your string resources to use the <annotation> tag instead of the <xliff:g> tag:
<string name="sentence">I like a <annotation id="fruit" example="apple">%s</annotation>.</string>
If you can do that, then you can retrieve the string resource as a SpannedString using getResources().getText() and then inspect the spans:
SpannedString spanned = (SpannedString) getResources().getText(R.string.sentence);
Annotation spans = spanned.getSpans(0, spanned.length(), Annotation.class);
int startPosition = spanned.getSpanStart(spans[0]);
Then you can format it for display:
String fruit = getString(R.string.fruit);
String sentence = String.format(spanned.toString(), fruit);
If you can't change your resources at all, you can do something similar by using getString() without extra parameters to retrieve the string with its format arguments (%s) and then use indexOf() on that:
String text = getResources().getString(R.string.sentence);
int startPosition = text.indexOf("%s");
And then format it for display the same way:
String fruit = getString(R.string.fruit);
String sentence = String.format(text, fruit);
This is worse for many reasons (the string could theoretically include "%s" as a non-format-argument substring, you need to know the exact specification of the format arguments ahead of time, etc). But it might be the best you can do if you can't change anything else.
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
My recommendation would be to change your string resources to use the <annotation> tag instead of the <xliff:g> tag:
<string name="sentence">I like a <annotation id="fruit" example="apple">%s</annotation>.</string>
If you can do that, then you can retrieve the string resource as a SpannedString using getResources().getText() and then inspect the spans:
SpannedString spanned = (SpannedString) getResources().getText(R.string.sentence);
Annotation spans = spanned.getSpans(0, spanned.length(), Annotation.class);
int startPosition = spanned.getSpanStart(spans[0]);
Then you can format it for display:
String fruit = getString(R.string.fruit);
String sentence = String.format(spanned.toString(), fruit);
If you can't change your resources at all, you can do something similar by using getString() without extra parameters to retrieve the string with its format arguments (%s) and then use indexOf() on that:
String text = getResources().getString(R.string.sentence);
int startPosition = text.indexOf("%s");
And then format it for display the same way:
String fruit = getString(R.string.fruit);
String sentence = String.format(text, fruit);
This is worse for many reasons (the string could theoretically include "%s" as a non-format-argument substring, you need to know the exact specification of the format arguments ahead of time, etc). But it might be the best you can do if you can't change anything else.
My recommendation would be to change your string resources to use the <annotation> tag instead of the <xliff:g> tag:
<string name="sentence">I like a <annotation id="fruit" example="apple">%s</annotation>.</string>
If you can do that, then you can retrieve the string resource as a SpannedString using getResources().getText() and then inspect the spans:
SpannedString spanned = (SpannedString) getResources().getText(R.string.sentence);
Annotation spans = spanned.getSpans(0, spanned.length(), Annotation.class);
int startPosition = spanned.getSpanStart(spans[0]);
Then you can format it for display:
String fruit = getString(R.string.fruit);
String sentence = String.format(spanned.toString(), fruit);
If you can't change your resources at all, you can do something similar by using getString() without extra parameters to retrieve the string with its format arguments (%s) and then use indexOf() on that:
String text = getResources().getString(R.string.sentence);
int startPosition = text.indexOf("%s");
And then format it for display the same way:
String fruit = getString(R.string.fruit);
String sentence = String.format(text, fruit);
This is worse for many reasons (the string could theoretically include "%s" as a non-format-argument substring, you need to know the exact specification of the format arguments ahead of time, etc). But it might be the best you can do if you can't change anything else.
answered Nov 9 at 22:35
Ben P.
21.1k31845
21.1k31845
add a comment |
add a comment |
up vote
0
down vote
You can split Your string like this:
String parts = sentence.split(" " + fruit + " ");
String part1 = parts[0];
int pos = part1.length() + 1;
part1 is a string that contains everything before " pear ", so its length is the position where word "pear" starts minus 1 (we need to use " " before and after the word to ensure that we will not find it as part of unrelated word in sentence).
That's some nice idea but this breaks for sentences like "I like pears as pears are my favorite fruit." (part1 = "I like",part2 = "as pears are my favorite fruit."- assuming it splits at first opportunity). So I wouldn't deem this as reliable.
– J. Doe
Nov 9 at 22:33
add a comment |
up vote
0
down vote
You can split Your string like this:
String parts = sentence.split(" " + fruit + " ");
String part1 = parts[0];
int pos = part1.length() + 1;
part1 is a string that contains everything before " pear ", so its length is the position where word "pear" starts minus 1 (we need to use " " before and after the word to ensure that we will not find it as part of unrelated word in sentence).
That's some nice idea but this breaks for sentences like "I like pears as pears are my favorite fruit." (part1 = "I like",part2 = "as pears are my favorite fruit."- assuming it splits at first opportunity). So I wouldn't deem this as reliable.
– J. Doe
Nov 9 at 22:33
add a comment |
up vote
0
down vote
up vote
0
down vote
You can split Your string like this:
String parts = sentence.split(" " + fruit + " ");
String part1 = parts[0];
int pos = part1.length() + 1;
part1 is a string that contains everything before " pear ", so its length is the position where word "pear" starts minus 1 (we need to use " " before and after the word to ensure that we will not find it as part of unrelated word in sentence).
You can split Your string like this:
String parts = sentence.split(" " + fruit + " ");
String part1 = parts[0];
int pos = part1.length() + 1;
part1 is a string that contains everything before " pear ", so its length is the position where word "pear" starts minus 1 (we need to use " " before and after the word to ensure that we will not find it as part of unrelated word in sentence).
answered Nov 9 at 22:21
Pavel B.
360210
360210
That's some nice idea but this breaks for sentences like "I like pears as pears are my favorite fruit." (part1 = "I like",part2 = "as pears are my favorite fruit."- assuming it splits at first opportunity). So I wouldn't deem this as reliable.
– J. Doe
Nov 9 at 22:33
add a comment |
That's some nice idea but this breaks for sentences like "I like pears as pears are my favorite fruit." (part1 = "I like",part2 = "as pears are my favorite fruit."- assuming it splits at first opportunity). So I wouldn't deem this as reliable.
– J. Doe
Nov 9 at 22:33
That's some nice idea but this breaks for sentences like "I like pears as pears are my favorite fruit." (
part1 = "I like", part2 = "as pears are my favorite fruit." - assuming it splits at first opportunity). So I wouldn't deem this as reliable.– J. Doe
Nov 9 at 22:33
That's some nice idea but this breaks for sentences like "I like pears as pears are my favorite fruit." (
part1 = "I like", part2 = "as pears are my favorite fruit." - assuming it splits at first opportunity). So I wouldn't deem this as reliable.– J. Doe
Nov 9 at 22:33
add a comment |
up vote
0
down vote
Use the indexOf method
int pos = sentence.indexOf(fruit);
copy-paste: That's some nice idea but this breaks for sentences like "I like pears as pears are my favorite fruit.". So I wouldn't deem this as reliable.
– J. Doe
Nov 9 at 22:36
add a comment |
up vote
0
down vote
Use the indexOf method
int pos = sentence.indexOf(fruit);
copy-paste: That's some nice idea but this breaks for sentences like "I like pears as pears are my favorite fruit.". So I wouldn't deem this as reliable.
– J. Doe
Nov 9 at 22:36
add a comment |
up vote
0
down vote
up vote
0
down vote
Use the indexOf method
int pos = sentence.indexOf(fruit);
Use the indexOf method
int pos = sentence.indexOf(fruit);
answered Nov 9 at 22:24
Adam
8612
8612
copy-paste: That's some nice idea but this breaks for sentences like "I like pears as pears are my favorite fruit.". So I wouldn't deem this as reliable.
– J. Doe
Nov 9 at 22:36
add a comment |
copy-paste: That's some nice idea but this breaks for sentences like "I like pears as pears are my favorite fruit.". So I wouldn't deem this as reliable.
– J. Doe
Nov 9 at 22:36
copy-paste: That's some nice idea but this breaks for sentences like "I like pears as pears are my favorite fruit.". So I wouldn't deem this as reliable.
– J. Doe
Nov 9 at 22:36
copy-paste: That's some nice idea but this breaks for sentences like "I like pears as pears are my favorite fruit.". So I wouldn't deem this as reliable.
– J. Doe
Nov 9 at 22:36
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233945%2fhow-do-i-get-the-position-of-a-substring-that-was-formatted-by-getstring%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
"Sadly changing the implementation of resources and data parsing is not an option" -- does this mean you can't change the string resources at all? Can you change the
xlifftag even?– Ben P.
Nov 9 at 22:18
I noticed a bug of a software in my native language and that it's present in other languages as well (actually it's implemented with
int pos = sentence.length() - fruit.length() - 1;right now). I want to fix this via code because otherwise every file for every language has to be modified. As thexlifftag is in the resources I'd say it's set in stone as well.– J. Doe
Nov 9 at 22:27