0

My String look like this

String url = 'domain1/balabala.mp3,domain2/aaaaa.mp3,domain3/baaaaa.mp3';

I want out put Like this

String domain1 = 'domain1/balabala.mp3';
String domain2 = 'domain2/aaaaa.mp3';
String domain3 = 'domain3/baaaaa.mp3';

Ex: Where host name called 'domain1' get the link as String

EDITED: I dont Want Just a Array. I need Out put based on domain host name. All Answer is good. Pls check again.

This one Nearly Work but not i Expected..

String[] separated = CurrentString.split(",");
separated[0]; // this will contain "domain1"
separated[1];// this will contain "domain2"

But this one Not Based host name.. It just separate first ,second and third domain name

  • 1
    have u tried String.split to split current string? – ρяσѕρєя K Mar 27 '13 at 09:56
  • All answers are good, but imho there is also StringTokenizer, take a look at this: http://stackoverflow.com/questions/691184/scanner-vs-stringtokenizer-vs-string-split . – JJ86 Mar 27 '13 at 10:10

3 Answers3

1

You could split on the comma, this gives you an array which makes more sense than declaring variables as your string I assume may contain a different number of domains.

final String url = "domain1/balabala.mp3,domain2/aaaaa.mp3,domain3/baaaaa.mp3";
final String[] split = url.split(",");
for(final String domain : split) {
    System.out.println(domain);
}
david99world
  • 18,271
  • 28
  • 102
  • 127
  • I need Separate String Name for each domain Example " IF LIKE '%domain1%' in String url " output is String domain1 = "domain1/balabala.mp3";. Sorry my bad English – user1901994 Mar 27 '13 at 10:09
1

Try split..

String[] domains = "domain1/balabala.mp3,domain2/aaaaa.mp3,domain3/baaaaa.mp3".split(",");
Nermeen
  • 15,570
  • 5
  • 52
  • 68
0
String url = "domain1/balabala.mp3,domain2/aaaaa.mp3,domain3/baaaaa.mp3";
    String[] strAry = url.split(",");
    for (int i=0; i<strAry.length; i++) {
        System.out.println(strAry[i]);
    }
dmnlk
  • 2,645
  • 1
  • 22
  • 29