• We just launched and are currently in beta. Join us as we build and grow the community.

Split a String in Java

CKHypers

Social Media Strategist
C Rep
0
0
0
Rep
0
C Vouches
0
0
0
Vouches
0
Posts
203
Likes
57
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
This tutorial will teach you how to create a program that will split a string using java.

So, now let's start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of splitString.java.

2. We will initialize variables in our Main, variable str as a String, and variable elements as a String array.

  1. String

    str =

    "monday,tuesday,wednesday,thursday,friday,saturday,sunday"

    ;

  2. String

    [

    ]

    elements;

3. To split the string in variable str, we will use the split method of the str with the elements of the String array.

  1. elements =

    str.split

    (

    ","

    )

    ;

As you can see, i have choose to have comma "," on the split keyword so that all the commas in the String will be omitted. The split keyword means to omit those characters in the string. Note: You can change the comma keyword into any characters that you want.

4. To display the text that was split, we will declare a for loop because the string now was an array.
  1. for

    (

    int

    x =

    0

    ;

    x <

    elements.length

    ;

    x++

    )

    {
  2. System

    .out

    .println

    (

    elements[

    x]

    )

    ;
  3. }


Output:

split.png


Here's the full code of this tutorial:

  1. public

    class

    splitString {
  2. public

    static

    void

    main(

    String

    [

    ]

    args)

    {

  3. String

    str =

    "monday,tuesday,wednesday,thursday,friday,saturday,sunday"

    ;

  4. String

    [

    ]

    elements;

  5. elements =

    str.split

    (

    ","

    )

    ;

  6. for

    (

    int

    x =

    0

    ;

    x <

    elements.length

    ;

    x++

    )

    {
  7. System

    .out

    .println

    (

    elements[

    x]

    )

    ;
  8. }


  9. }
  10. }

For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.

Best Regards,

Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware


Download
You must upgrade your account or reply in the thread to view hidden text.
 

442,401

317,942

317,951

Top