-8

I have a string after scan QR code is "MAT:TO:My address email;SUB:My title;BODY:My content;;". How to split address email, your title and your content from string this? Thank you all everyone!

Subhanshuja
  • 390
  • 1
  • 3
  • 20
Nguyen Quoc
  • 3
  • 1
  • 1
  • 3

2 Answers2

7

Java version

String s = "MAT:TO:My address email;SUB:My title;BODY:My content;;";
String[] arrayString = s.split(";");

String email = arrayString[0];
String title = arrayString[1];
String body = arrayString[2];

email= email.substring(email.indexOf("MAT:TO:") + 7, email.length());
title= title.substring(title.indexOf("SUB:") + 4, title.length());
body= body.substring(body.indexOf("BODY:") + 5, body.length());

Especially Android official is used kotlin version:

val s = "MAT:TO:My address email;SUB:My title;BODY:My content;;"
val arrayString = s.split(";").toTypedArray()

var email = arrayString[0]
var title = arrayString[1]
var body = arrayString[2]

email = email.substring(email.indexOf("MAT:TO:") + 7, email.length)
title = title.substring(title.indexOf("SUB:") + 4, title.length)
body = body.substring(body.indexOf("BODY:") + 5, body.length)
Subhanshuja
  • 390
  • 1
  • 3
  • 20
Nithinlal
  • 4,845
  • 1
  • 29
  • 40
1

Try using the split() method.

If you want to split by ";"

String[] arrayString = string.split(";");

In your case you would get

["MAT:TO:My address email", "SUB:My title", "BODY:My content"] 

and then split by ":" or the other way around, whichever better suits you.

String email = arrayString[0].split(":")[2];
String title = arrayString[1].split(":")[1];
String body = arrayString[2].split(":")[1];

This is a bad way to do it, not very safe.

Or you could use string.substring(int startIndex, int endIndex).

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Marko
  • 20,385
  • 13
  • 48
  • 64
  • Can you give me an example? because I beginning with java. Thank you! – Nguyen Quoc Sep 11 '15 at 06:22
  • Added the revelant code. – Marko Sep 11 '15 at 06:30
  • That's great! Thank you very much! – Nguyen Quoc Sep 11 '15 at 06:41
  • String string = "MAIL:TO:My email address, SUB:My title, BODY:My content"; String[] parts = string.split(":"); String mail = parts[0]; String to = parts[1]; String part3 = parts[2]; String[] array = part3.split(","); String emailAddress = array[0]; String subjectTitle = array[0]; String part4 = parts[3]; String[] array2 = part4.split(","); String subject = array[0]; String bodyTitle = array[1]; String body = parts[4]; – Abdul Rahman Majeed Sep 11 '15 at 06:41