Tuesday, January 8, 2013

Java : Converting Strings to Array of String

Hello everyone? How are you doing? I've been doing lot's of stuffs and practicing my JAVA skills because I only knew a little in this programming language and since it's a good language I want to put it in my priority list and try to master the language. I've been solving Programming Problems lately and I realize that I need to
make or convert my String into an Array of String. So I asked GOOGLE on how to do it in the easiest way. Then I found out the method split(). Using this method you can explode a string into array, you have to specify on how you want to explode it, for example you want to make this string "10/11/12" into an array and explode it using the "/" so the array elements are "{10, 11, 12}" you will do "10/11/12".split("/"). Sounds great right? Here's an example.


class StringToArray {
    public static void main(String args[]) {
          String myString = "alleo";
          String myArray[] = myString.split("");


          //display output
          for(String x : myArray) {
             System.out.println(x);
          }
    }
}


Output :

a
l
l
e
o

It's so good right? give it a try guys and practice it, you can do something great with that function! Happy Programming :) 

2 comments:

  1. whoa... we're in the same field ... i remember doing that code,(not exact though) in python... :D.

    ReplyDelete