0

this is the example text int text file

title: get this string and desc: get this string

I want to split it with "title:" and "desc:"

user3325919
  • 45
  • 1
  • 7

2 Answers2

1

it is simple : after getting file content (https://stackoverflow.com/a/14768380/1725748), do :

String mystring= "title: xxxxxx desc: yyyyy";
String[] splits = mystring.split("title:|desc:");

splits[0] // is the title
splits[1] // is the description
Community
  • 1
  • 1
OWZY
  • 531
  • 7
  • 15
  • how about if the text file has multiple lines? Example: title: sadsa desc: dsfsdfds title: sadsa desc: dsfsdfds title: sadsa desc: dsfsdfds – user3325919 Feb 19 '14 at 01:37
  • If you want to add more separators just add theme like that : mystring.split("title:|desc:|date:|comment: ...."); – OWZY Feb 19 '14 at 14:00
0

There are various ways to get a String how you like, here I found the index of desc and split the String where desc appears:

    try{
        InputStream inputStream = getResources().openRawResource(R.raw.textfile);
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String line;
        String title_content, desc_content;

        // Read each line
        while((line = reader.readLine( )) != null)
        { 
            // title: get this string and desc: get this string
            //                            ^
            //                     (desc_location)

            int desc_location;
            if((desc_location = line.indexOf("desc:")) > 0)
            {
                title_content = line.substring(0, desc_location);
                desc_content  = line.substring(desc_location, line.length( )); 
            }
        }
    } catch (Exception e) {
        // e.printStackTrace();
    }
jak10h
  • 519
  • 4
  • 11