Getting rid of half a String in Android











up vote
1
down vote

favorite












Need help with getting rid of half of a string in android studio. The string is:



final String strOrigin = String.valueOf(origin).trim();


The value that is returned is;



"Point{type=Point, bbox=null, coordinates=[27.993726079654873,-26.14686805145815]}"



I want to be left with only the numbers of that, in the String. I have tried;



strOrigin.replace("Point{type=Point, bbox=null, coordinates=", "");


But it isn't working. Any help would be appreciated.










share|improve this question
























  • strOrigin.replaceAll("\D+",""); if you want to use regrex to get just digits of a string.
    – SnakeyHips
    Nov 22 at 14:27












  • Isn't there an option to just get the coordinates? Something like final String origin = origin.getCoordinates().toString(); ?
    – PPartisan
    Nov 22 at 14:30










  • What is origin?
    – TheWanderer
    Nov 22 at 14:35










  • @TheWanderer Origin is the variable for a Mapbox co-ordinate, and i want to save those co-ordinates in a database.
    – Alexander Mcmichael
    Nov 22 at 14:50












  • That's not very helpful. What is the Origin object and where is it from?
    – TheWanderer
    Nov 22 at 14:51















up vote
1
down vote

favorite












Need help with getting rid of half of a string in android studio. The string is:



final String strOrigin = String.valueOf(origin).trim();


The value that is returned is;



"Point{type=Point, bbox=null, coordinates=[27.993726079654873,-26.14686805145815]}"



I want to be left with only the numbers of that, in the String. I have tried;



strOrigin.replace("Point{type=Point, bbox=null, coordinates=", "");


But it isn't working. Any help would be appreciated.










share|improve this question
























  • strOrigin.replaceAll("\D+",""); if you want to use regrex to get just digits of a string.
    – SnakeyHips
    Nov 22 at 14:27












  • Isn't there an option to just get the coordinates? Something like final String origin = origin.getCoordinates().toString(); ?
    – PPartisan
    Nov 22 at 14:30










  • What is origin?
    – TheWanderer
    Nov 22 at 14:35










  • @TheWanderer Origin is the variable for a Mapbox co-ordinate, and i want to save those co-ordinates in a database.
    – Alexander Mcmichael
    Nov 22 at 14:50












  • That's not very helpful. What is the Origin object and where is it from?
    – TheWanderer
    Nov 22 at 14:51













up vote
1
down vote

favorite









up vote
1
down vote

favorite











Need help with getting rid of half of a string in android studio. The string is:



final String strOrigin = String.valueOf(origin).trim();


The value that is returned is;



"Point{type=Point, bbox=null, coordinates=[27.993726079654873,-26.14686805145815]}"



I want to be left with only the numbers of that, in the String. I have tried;



strOrigin.replace("Point{type=Point, bbox=null, coordinates=", "");


But it isn't working. Any help would be appreciated.










share|improve this question















Need help with getting rid of half of a string in android studio. The string is:



final String strOrigin = String.valueOf(origin).trim();


The value that is returned is;



"Point{type=Point, bbox=null, coordinates=[27.993726079654873,-26.14686805145815]}"



I want to be left with only the numbers of that, in the String. I have tried;



strOrigin.replace("Point{type=Point, bbox=null, coordinates=", "");


But it isn't working. Any help would be appreciated.







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 15:21









Milo Bem

792318




792318










asked Nov 22 at 14:19









Alexander Mcmichael

267




267












  • strOrigin.replaceAll("\D+",""); if you want to use regrex to get just digits of a string.
    – SnakeyHips
    Nov 22 at 14:27












  • Isn't there an option to just get the coordinates? Something like final String origin = origin.getCoordinates().toString(); ?
    – PPartisan
    Nov 22 at 14:30










  • What is origin?
    – TheWanderer
    Nov 22 at 14:35










  • @TheWanderer Origin is the variable for a Mapbox co-ordinate, and i want to save those co-ordinates in a database.
    – Alexander Mcmichael
    Nov 22 at 14:50












  • That's not very helpful. What is the Origin object and where is it from?
    – TheWanderer
    Nov 22 at 14:51


















  • strOrigin.replaceAll("\D+",""); if you want to use regrex to get just digits of a string.
    – SnakeyHips
    Nov 22 at 14:27












  • Isn't there an option to just get the coordinates? Something like final String origin = origin.getCoordinates().toString(); ?
    – PPartisan
    Nov 22 at 14:30










  • What is origin?
    – TheWanderer
    Nov 22 at 14:35










  • @TheWanderer Origin is the variable for a Mapbox co-ordinate, and i want to save those co-ordinates in a database.
    – Alexander Mcmichael
    Nov 22 at 14:50












  • That's not very helpful. What is the Origin object and where is it from?
    – TheWanderer
    Nov 22 at 14:51
















strOrigin.replaceAll("\D+",""); if you want to use regrex to get just digits of a string.
– SnakeyHips
Nov 22 at 14:27






strOrigin.replaceAll("\D+",""); if you want to use regrex to get just digits of a string.
– SnakeyHips
Nov 22 at 14:27














Isn't there an option to just get the coordinates? Something like final String origin = origin.getCoordinates().toString(); ?
– PPartisan
Nov 22 at 14:30




Isn't there an option to just get the coordinates? Something like final String origin = origin.getCoordinates().toString(); ?
– PPartisan
Nov 22 at 14:30












What is origin?
– TheWanderer
Nov 22 at 14:35




What is origin?
– TheWanderer
Nov 22 at 14:35












@TheWanderer Origin is the variable for a Mapbox co-ordinate, and i want to save those co-ordinates in a database.
– Alexander Mcmichael
Nov 22 at 14:50






@TheWanderer Origin is the variable for a Mapbox co-ordinate, and i want to save those co-ordinates in a database.
– Alexander Mcmichael
Nov 22 at 14:50














That's not very helpful. What is the Origin object and where is it from?
– TheWanderer
Nov 22 at 14:51




That's not very helpful. What is the Origin object and where is it from?
– TheWanderer
Nov 22 at 14:51












5 Answers
5






active

oldest

votes

















up vote
4
down vote



accepted










In Java strings are immutable. You must assign the result to a new string:



String strResult = strOrigin.replace("Point{type=Point, bbox=null, coordinates=", "");





share|improve this answer























  • Thank you, works nicely!
    – Alexander Mcmichael
    Nov 22 at 14:42


















up vote
2
down vote













Maybe you forgot that replace() returns the result and you must assign it to a string:



String strOrigin = "Point{type=Point, bbox=null, coordinates=[27.993726079654873,-26.14686805145815]}";
String str = strOrigin
.replace("Point{type=Point, bbox=null, coordinates=", "")
.replace("}","");
System.out.println(str);


will print:



[27.993726079654873,-26.14686805145815]





share|improve this answer




























    up vote
    2
    down vote













    Make your life simpler by just getting the coordinates:



    final String strOigin = origin.coordinates().stream()
    .map(String::valueOf)
    .collect(Collectors.joining(",","{","}"));


    Or, if you're stuck on java 7:



    final String strOigin = String.format(
    "{%s,%s}",
    String.valueOf(origin.latitude()),
    String.valueOf(origin.longitude())
    );





    share|improve this answer



















    • 1




      Stream is a Java 8 feature. Most Android apps are using Java 7, unless they're only targeting Nougat and later.
      – TheWanderer
      Nov 22 at 14:34










    • Alright, I've added an answer for Java 7
      – PPartisan
      Nov 22 at 14:38


















    up vote
    1
    down vote













    This should do the trick



        String s="blabla coordinates=[27.993726079654873,-26.14686805145815] ";
    String requiredString = s.substring(s.indexOf("[") + 1, s.indexOf("]"));


    will print:



    27.993726079654873,-26.14686805145815


    Than you can cast it to double or latlong format






    share|improve this answer




























      up vote
      1
      down vote













      Have a look at regular expressions, they allow you to define more flexible search patterns. In your example you only find the coordinates if the rest of the string matches the pattern exactly, but if you happen to get some other value of bbox, or even extra space it will not work. This will always match everything between a pair of square brackets:



      String c = origin.replaceAll(".+?(\[.+?\]).+?", "$1");


      http://www.vogella.com/tutorials/JavaRegularExpressions/article.html
      https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html






      share|improve this answer





















        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%2f53432953%2fgetting-rid-of-half-a-string-in-android%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        4
        down vote



        accepted










        In Java strings are immutable. You must assign the result to a new string:



        String strResult = strOrigin.replace("Point{type=Point, bbox=null, coordinates=", "");





        share|improve this answer























        • Thank you, works nicely!
          – Alexander Mcmichael
          Nov 22 at 14:42















        up vote
        4
        down vote



        accepted










        In Java strings are immutable. You must assign the result to a new string:



        String strResult = strOrigin.replace("Point{type=Point, bbox=null, coordinates=", "");





        share|improve this answer























        • Thank you, works nicely!
          – Alexander Mcmichael
          Nov 22 at 14:42













        up vote
        4
        down vote



        accepted







        up vote
        4
        down vote



        accepted






        In Java strings are immutable. You must assign the result to a new string:



        String strResult = strOrigin.replace("Point{type=Point, bbox=null, coordinates=", "");





        share|improve this answer














        In Java strings are immutable. You must assign the result to a new string:



        String strResult = strOrigin.replace("Point{type=Point, bbox=null, coordinates=", "");






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 at 14:28









        ZUNJAE

        6341618




        6341618










        answered Nov 22 at 14:22









        Psytho

        1,8711819




        1,8711819












        • Thank you, works nicely!
          – Alexander Mcmichael
          Nov 22 at 14:42


















        • Thank you, works nicely!
          – Alexander Mcmichael
          Nov 22 at 14:42
















        Thank you, works nicely!
        – Alexander Mcmichael
        Nov 22 at 14:42




        Thank you, works nicely!
        – Alexander Mcmichael
        Nov 22 at 14:42












        up vote
        2
        down vote













        Maybe you forgot that replace() returns the result and you must assign it to a string:



        String strOrigin = "Point{type=Point, bbox=null, coordinates=[27.993726079654873,-26.14686805145815]}";
        String str = strOrigin
        .replace("Point{type=Point, bbox=null, coordinates=", "")
        .replace("}","");
        System.out.println(str);


        will print:



        [27.993726079654873,-26.14686805145815]





        share|improve this answer

























          up vote
          2
          down vote













          Maybe you forgot that replace() returns the result and you must assign it to a string:



          String strOrigin = "Point{type=Point, bbox=null, coordinates=[27.993726079654873,-26.14686805145815]}";
          String str = strOrigin
          .replace("Point{type=Point, bbox=null, coordinates=", "")
          .replace("}","");
          System.out.println(str);


          will print:



          [27.993726079654873,-26.14686805145815]





          share|improve this answer























            up vote
            2
            down vote










            up vote
            2
            down vote









            Maybe you forgot that replace() returns the result and you must assign it to a string:



            String strOrigin = "Point{type=Point, bbox=null, coordinates=[27.993726079654873,-26.14686805145815]}";
            String str = strOrigin
            .replace("Point{type=Point, bbox=null, coordinates=", "")
            .replace("}","");
            System.out.println(str);


            will print:



            [27.993726079654873,-26.14686805145815]





            share|improve this answer












            Maybe you forgot that replace() returns the result and you must assign it to a string:



            String strOrigin = "Point{type=Point, bbox=null, coordinates=[27.993726079654873,-26.14686805145815]}";
            String str = strOrigin
            .replace("Point{type=Point, bbox=null, coordinates=", "")
            .replace("}","");
            System.out.println(str);


            will print:



            [27.993726079654873,-26.14686805145815]






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 at 14:28









            forpas

            6,0881218




            6,0881218






















                up vote
                2
                down vote













                Make your life simpler by just getting the coordinates:



                final String strOigin = origin.coordinates().stream()
                .map(String::valueOf)
                .collect(Collectors.joining(",","{","}"));


                Or, if you're stuck on java 7:



                final String strOigin = String.format(
                "{%s,%s}",
                String.valueOf(origin.latitude()),
                String.valueOf(origin.longitude())
                );





                share|improve this answer



















                • 1




                  Stream is a Java 8 feature. Most Android apps are using Java 7, unless they're only targeting Nougat and later.
                  – TheWanderer
                  Nov 22 at 14:34










                • Alright, I've added an answer for Java 7
                  – PPartisan
                  Nov 22 at 14:38















                up vote
                2
                down vote













                Make your life simpler by just getting the coordinates:



                final String strOigin = origin.coordinates().stream()
                .map(String::valueOf)
                .collect(Collectors.joining(",","{","}"));


                Or, if you're stuck on java 7:



                final String strOigin = String.format(
                "{%s,%s}",
                String.valueOf(origin.latitude()),
                String.valueOf(origin.longitude())
                );





                share|improve this answer



















                • 1




                  Stream is a Java 8 feature. Most Android apps are using Java 7, unless they're only targeting Nougat and later.
                  – TheWanderer
                  Nov 22 at 14:34










                • Alright, I've added an answer for Java 7
                  – PPartisan
                  Nov 22 at 14:38













                up vote
                2
                down vote










                up vote
                2
                down vote









                Make your life simpler by just getting the coordinates:



                final String strOigin = origin.coordinates().stream()
                .map(String::valueOf)
                .collect(Collectors.joining(",","{","}"));


                Or, if you're stuck on java 7:



                final String strOigin = String.format(
                "{%s,%s}",
                String.valueOf(origin.latitude()),
                String.valueOf(origin.longitude())
                );





                share|improve this answer














                Make your life simpler by just getting the coordinates:



                final String strOigin = origin.coordinates().stream()
                .map(String::valueOf)
                .collect(Collectors.joining(",","{","}"));


                Or, if you're stuck on java 7:



                final String strOigin = String.format(
                "{%s,%s}",
                String.valueOf(origin.latitude()),
                String.valueOf(origin.longitude())
                );






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 22 at 14:37

























                answered Nov 22 at 14:34









                PPartisan

                4,62541636




                4,62541636








                • 1




                  Stream is a Java 8 feature. Most Android apps are using Java 7, unless they're only targeting Nougat and later.
                  – TheWanderer
                  Nov 22 at 14:34










                • Alright, I've added an answer for Java 7
                  – PPartisan
                  Nov 22 at 14:38














                • 1




                  Stream is a Java 8 feature. Most Android apps are using Java 7, unless they're only targeting Nougat and later.
                  – TheWanderer
                  Nov 22 at 14:34










                • Alright, I've added an answer for Java 7
                  – PPartisan
                  Nov 22 at 14:38








                1




                1




                Stream is a Java 8 feature. Most Android apps are using Java 7, unless they're only targeting Nougat and later.
                – TheWanderer
                Nov 22 at 14:34




                Stream is a Java 8 feature. Most Android apps are using Java 7, unless they're only targeting Nougat and later.
                – TheWanderer
                Nov 22 at 14:34












                Alright, I've added an answer for Java 7
                – PPartisan
                Nov 22 at 14:38




                Alright, I've added an answer for Java 7
                – PPartisan
                Nov 22 at 14:38










                up vote
                1
                down vote













                This should do the trick



                    String s="blabla coordinates=[27.993726079654873,-26.14686805145815] ";
                String requiredString = s.substring(s.indexOf("[") + 1, s.indexOf("]"));


                will print:



                27.993726079654873,-26.14686805145815


                Than you can cast it to double or latlong format






                share|improve this answer

























                  up vote
                  1
                  down vote













                  This should do the trick



                      String s="blabla coordinates=[27.993726079654873,-26.14686805145815] ";
                  String requiredString = s.substring(s.indexOf("[") + 1, s.indexOf("]"));


                  will print:



                  27.993726079654873,-26.14686805145815


                  Than you can cast it to double or latlong format






                  share|improve this answer























                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    This should do the trick



                        String s="blabla coordinates=[27.993726079654873,-26.14686805145815] ";
                    String requiredString = s.substring(s.indexOf("[") + 1, s.indexOf("]"));


                    will print:



                    27.993726079654873,-26.14686805145815


                    Than you can cast it to double or latlong format






                    share|improve this answer












                    This should do the trick



                        String s="blabla coordinates=[27.993726079654873,-26.14686805145815] ";
                    String requiredString = s.substring(s.indexOf("[") + 1, s.indexOf("]"));


                    will print:



                    27.993726079654873,-26.14686805145815


                    Than you can cast it to double or latlong format







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 22 at 14:29









                    Hossam Hassan

                    1401116




                    1401116






















                        up vote
                        1
                        down vote













                        Have a look at regular expressions, they allow you to define more flexible search patterns. In your example you only find the coordinates if the rest of the string matches the pattern exactly, but if you happen to get some other value of bbox, or even extra space it will not work. This will always match everything between a pair of square brackets:



                        String c = origin.replaceAll(".+?(\[.+?\]).+?", "$1");


                        http://www.vogella.com/tutorials/JavaRegularExpressions/article.html
                        https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html






                        share|improve this answer

























                          up vote
                          1
                          down vote













                          Have a look at regular expressions, they allow you to define more flexible search patterns. In your example you only find the coordinates if the rest of the string matches the pattern exactly, but if you happen to get some other value of bbox, or even extra space it will not work. This will always match everything between a pair of square brackets:



                          String c = origin.replaceAll(".+?(\[.+?\]).+?", "$1");


                          http://www.vogella.com/tutorials/JavaRegularExpressions/article.html
                          https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html






                          share|improve this answer























                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            Have a look at regular expressions, they allow you to define more flexible search patterns. In your example you only find the coordinates if the rest of the string matches the pattern exactly, but if you happen to get some other value of bbox, or even extra space it will not work. This will always match everything between a pair of square brackets:



                            String c = origin.replaceAll(".+?(\[.+?\]).+?", "$1");


                            http://www.vogella.com/tutorials/JavaRegularExpressions/article.html
                            https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html






                            share|improve this answer












                            Have a look at regular expressions, they allow you to define more flexible search patterns. In your example you only find the coordinates if the rest of the string matches the pattern exactly, but if you happen to get some other value of bbox, or even extra space it will not work. This will always match everything between a pair of square brackets:



                            String c = origin.replaceAll(".+?(\[.+?\]).+?", "$1");


                            http://www.vogella.com/tutorials/JavaRegularExpressions/article.html
                            https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 22 at 14:54









                            Milo Bem

                            792318




                            792318






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432953%2fgetting-rid-of-half-a-string-in-android%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

                                What visual should I use to simply compare current year value vs last year in Power BI desktop

                                How to ignore python UserWarning in pytest?

                                Alexandru Averescu