Java explode string / string e.g. php explode

I am making a java program that runs on a local server.

The server accepts the request from the client using PHP.

<?php $file = fopen('temp.txt', 'a+'); $a=explode(':',$_GET['content']); fwrite($file,$a[0].':'.$a[1]. '\n'); fclose($file); ?> 

Now I have a temp.txt file on the local server.

A Java program should open a file that is read line by line, and each of them should be split / blown ":" (There is only one ":" in a line)

I tried differently, but could not, just like PHP breaks a string.

Is it possible to use the same / similar break function in JAVA.

+8
java php
source share
2 answers

Yes, in Java you can use the String # split (String regex) method to split the value of a String object.

Update : For example:

 String arr = "name:password"; String[] split = arr.split(":"); System.out.println("Name = " + split[0]); System.out.println("Password = " + split[1]); 
+14
source share

You can use String.split in Java to "explode" each line with ":".

Edit

Example for one line:

 String line = "one:two:three"; String[] words = line.split(":"); for (String word: words) { System.out.println(word); } 

Exit:

 one two three 
+4
source share

All Articles