-1

So im making this txt document that a string writes too with diffrent information about 100-2000 lines . From this I want to be able to copy specific parts such as a name and write them too a new file so I was wondering if someone could help do a example

I want just name,adress and phone pullout of a string as example downbelow and write it to a file :

nameadress=johndoe&adress=newyork163thdowntown&phone=+341431242

basicly I want find nameadress ,adress ,phone copy everything between = and &

and so on and if there's garbage data inbetween i want that ignored :) so I only get these :)

  • in android app u wanna do this? – KOTIOS Aug 11 '14 at 19:03
  • Yes :) learning android so this is a fun project for me too try out but i got stuck on how to do it for android as the examples i find aren't exactly how I want it done and I dont know how to make the logic :( – AmazingHorse Aug 11 '14 at 19:06
  • @AmazingHorse Split the string by `&`, then split each chunk by `=` – BackSlash Aug 11 '14 at 19:07
  • Is there a way to make it more versitile and by searching for nameadress=and just coping everything from = to & ? – AmazingHorse Aug 11 '14 at 19:09
  • I think you'll find your question is answered here: http://stackoverflow.com/questions/1129996/how-do-i-create-a-regular-expression-for-this-in-android – Masterfool Aug 11 '14 at 19:11
  • Can you create an example ? :) Couldnt exactly understand how that wokred – AmazingHorse Aug 11 '14 at 19:14
  • use any of the parse methods from URLEncodedUtils: http://developer.android.com/reference/org/apache/http/client/utils/URLEncodedUtils.html#parse(java.util.List, java.util.Scanner, java.lang.String) – njzk2 Aug 11 '14 at 19:22

2 Answers2

0

You can do it with str.split():

String str = "nameadress=johndoe&adress=newyork163thdowntown&phone=+341431242";
String[] firstSplit = str.split("&");

System.out.println(firstSplit[0]); // Prints nameadress=johndoe
System.out.println(firstSplit[1]); // Prints adress=newyork163thdowntown
System.out.println(firstSplit[2]); // Prints phone=+341431242

String name = firstSplit[0].split("=")[1]; // johndoe
String address = firstSplit[1].split("=")[1]; // newyork163thdowntown
String phone = firstSplit[2].split("=")[1]; // +341431242

Then you can write these three strings to a file

In order to ignore any other data you could do the following:

String str = "nameadress=johndoe&age=15&sex=female&adress=newyork163thdowntown&phone=+341431242";
String name = str.split("nameadress=")[1].split("&")[0];
String adress = str.split("adress=")[1].split("&")[0];
String phone = str.split("phone=")[1]; // This is the end of the String so we do not need to split("&")
Zach
  • 4,652
  • 18
  • 22
  • oh nice solution! :) might be an stupied question now but what happens if there is garage data inbetween for example name and adress such as sex and age and I dont want them :nameadress=johndoe&age=15&sex=female&adress=newyork163thdowntown&phone=+341431242 – AmazingHorse Aug 11 '14 at 19:19
  • do you have a well-defined structure for what the data can be? because currently it is linked to the index. – EpicPandaForce Aug 11 '14 at 19:20
  • Sorry, I misunderstood your question, in that case, `firstSplit[1]` would be `age=15` and `firstSplit[2]` would be `sex=female`. Everything else would get bumped down 2 indices in the firstSplit list to make space for the two new pieces of data. – Zach Aug 11 '14 at 19:21
  • oh going too try it :) – AmazingHorse Aug 11 '14 at 19:29
  • Updated my post also :) – AmazingHorse Aug 11 '14 at 19:32
  • I have updated my answer now. Make sure to accept it if it solves your problem :) – Zach Aug 11 '14 at 19:38
0

This sounds like something you'd want a parser for. See this answer for a good example of using the Guava libraries. Guava: Splitter and considering Escaping?

You would be able to use that to split your string by the '&' characters (and take into account any escape character you use). Once you have split them into:

nameadress=johndoe

adress=newyork163thdowntown

phone=+341431242

it is fairly simple to further split each string with the Guava Splitter class.

Alternatively, you could instead serialize to JSON, which there are libraries for.

Community
  • 1
  • 1
Trevor Siemens
  • 629
  • 5
  • 10
  • Thanks! Gonna try it but is there a way doing this without importing extenal libs? :) – AmazingHorse Aug 11 '14 at 19:30
  • You could write your own parser rather easily. Essentially going through each character and checking if it is '&', and whether or not the previous character is an escape character or not. If it is a valid separator character, you break off the characters you've read into a new string, and continue. Though I usually recommend importing the Guava libraries to any Java project, because they are incredibly useful. – Trevor Siemens Aug 11 '14 at 19:43
  • Any links on how too do it ? I've tried Guava and it works for spliting the words without a hitch but I would love to learn the logic behind how do everything :) – AmazingHorse Aug 11 '14 at 19:54
  • I don't know of any simple implementations which could serve as an example, sorry. – Trevor Siemens Aug 11 '14 at 20:25