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!
Asked
Active
Viewed 3.0k times
-8
-
1did you try splitting? – Rustam Sep 11 '15 at 06:05
-
http://stackoverflow.com/questions/32515417/how-do-i-split-the-string-of-url-link/32515547#32515547 – Nithinlal Sep 11 '15 at 06:07
-
I tried but it don't work String s = "MAT:TO:abc@gmail.com, xyz@gmail.com;SUB:Example;BODY:This is a string;;"; StringTokenizer st = new StringTokenizer(s, "MAT:TO;SUBBODY"); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); } – Nguyen Quoc Sep 11 '15 at 06:12
-
can you say the exact out put you want ? – Nithinlal Sep 11 '15 at 06:16
-
I want to output is: - abc@gmail.com - Example - This is a string – Nguyen Quoc Sep 11 '15 at 06:28
-
Accept the correct answer it will help other to solve issue – Nithinlal Sep 11 '15 at 07:03
2 Answers
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
-
Please, what is work of +7 in the first line email = email.substring(email.indexOf("MAT:TO:") + 7, email.length) – chisom emmanuel May 15 '22 at 21:38
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
-
-
-
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