java


Android converting string to array string [closed]


I have a string like this:
["477","com.dummybilling","android.test.purchased","inapp:com.dummybilling:android.test.purchased","779"]
How to have a String[] with these 5 element?
Does anyone know a regex for .split() method?
Thank you very much, regular expressions make me crazy! :(
Process it as JSON. Two immediate benifits would be that it would take care of any embedded commas in your data automatically and the other that you would get a String[] with unquoted strings.
String input = "[\"477\",\"com.dummybilling\",\"android.test.purchased\",\"inapp:com.dummybilling:android.test.purchased\",\"779\"]";
JSONArray jsonArray = new JSONArray(input);
String[] strArr = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
strArr[i] = jsonArray.getString(i);
}
System.out.println(Arrays.toString(strArr));
Output :
[477, com.dummybilling, android.test.purchased, inapp:com.dummybilling:android.test.purchased, 779]
You can split your string by separator [" (the beginning) or "," or "] (the ending) like this:
final String[] tokens = yourString.split("\",\"|\\[\"|\"\\]");
Please note that this will only work for your string. It's not a general solution (for example, it does not take care of any escaped quotes). If your string is in JSON format, you should use a JSON parser as proposed by #Ravi Thapliyal .

Related Links

IntelliJ Smart Line Join works with Java but not with Scala
HtmlUnit: how to search for text
Is it possible to setText of TextView in MainActivity.java using string.xml?
Why is my Java applet just displaying my fallback message?
Spring Boot Liquibase not picking up all application.yml files
Using BufferedReader.lines() breaks in parallel
log4j - Console log all modules in project
Android JSON Parsing - End of input at character 0 of error
Using generics for a class how can I convert the values from one type to another?
JtextArea Not Updating Itself
Executing multiple shell scripts on multiple remote servers at the same time using a java program
Fail testNG #Test before assertion is evaluated
Spring Data JPA: Cannot save entity with composite primary key which contains foreign key
Android ListView can't scroll more, bad custom Adapter?
Multiple clauses in MERGE for Oracle SQL: UPSERT AND DELETE in a single statement
EasyMock test an exception is thrown in the middle of code

Categories

HOME
cakephp
pandas
client
gitlab
netsuite
smarty
json.net
microservices
packages
ip
networkx
constraint-programming
podio
adfs
amazon-cloudformation
iggrid
facebook-php-sdk
nstableview
etl
static-libraries
jsprit
shared-hosting
alpine
opencover
sqlcipher
xpages-ssjs
sparse-matrix
jspm
primitive
r-raster
frame
preg-match-all
ioio
lxd
cloud-code
catalog
simplexml
space-complexity
streamsets
vapor
awt
dism
http-live-streaming
glew
eigenvalue
node-sass
taffy
chain-builder
apache-fop
promela
hendrix
pnotify
winscp-net
composite-key
knpmenubundle
capacity
chord-diagram
firmata
android-textview
dstu2-fhir
underscore.js-templating
mikroc
xml-attribute
bluemix-app-scan
hsv
ora-00900
thredds
libressl
tableau-online
apache-commons-fileupload
sonarqube5.1.2
umbraco6
rake-task
android-nested-fragment
googlemock
typeof
jubula
typo3-neos
quantlib-swig
commoncrypto
aqtime
django-nonrel
angularjs-controller
pvrtc
first-responder
table-footer
locate
mysql-error-1005
nhibernate.search
sector
ugc
windows-live-messenger

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App