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.










share|improve this question






















  • "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















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.










share|improve this question






















  • "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













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.










share|improve this question













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 android






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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


















  • "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
















"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












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.






share|improve this answer




























    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).






    share|improve this answer





















    • 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


















    up vote
    0
    down vote













    Use the indexOf method



    int pos = sentence.indexOf(fruit);





    share|improve this answer





















    • 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











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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.






    share|improve this answer

























      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.






      share|improve this answer























        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 22:35









        Ben P.

        21.1k31845




        21.1k31845
























            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).






            share|improve this answer





















            • 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















            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).






            share|improve this answer





















            • 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













            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).






            share|improve this answer












            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).







            share|improve this answer












            share|improve this answer



            share|improve this answer










            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


















            • 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










            up vote
            0
            down vote













            Use the indexOf method



            int pos = sentence.indexOf(fruit);





            share|improve this answer





















            • 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















            up vote
            0
            down vote













            Use the indexOf method



            int pos = sentence.indexOf(fruit);





            share|improve this answer





















            • 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













            up vote
            0
            down vote










            up vote
            0
            down vote









            Use the indexOf method



            int pos = sentence.indexOf(fruit);





            share|improve this answer












            Use the indexOf method



            int pos = sentence.indexOf(fruit);






            share|improve this answer












            share|improve this answer



            share|improve this answer










            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


















            • 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


















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            How to pass form data using jquery Ajax to insert data in database?

            National Museum of Racing and Hall of Fame

            Guess what letter conforming each word