Results 1 to 11 of 11
  1. #1
    nmvictor is offline Member
    Join Date
    Oct 2009
    Posts
    31
    Rep Power
    0

    Unhappy I wish to split a string xxx/yyyy/zzz so that ...

    Hi, I wish to split a string xxx/yyyy/zzzz so that the parts xxx yyyy and zzzz are returned as seperate strings on their own.Currently i am using the code below though i feel like their could be a better option with the method
    Java Code:
     split( )
    here what im using
    Java Code:
    //method to split xxx/yyyy/zzzz as seperate strings...
    public void getXYZ( )
    { 
    String str = "xxx/yyyy/zzzz";
    String x,  //for the xxx part
             y,  //for the yyyy part
             z;  //for the zzzz part
    x = str.substring(0,2);
    y = str.substring(4,8);
    z = str.substring(10,13);
    }
    I know some java geek has a better idea to this,possibly using
    Java Code:
     split( )
    , that will be really appreciated .Thanks in advance

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Yep, use String.split(regex)
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    nmvictor is offline Member
    Join Date
    Oct 2009
    Posts
    31
    Rep Power
    0

    Default

    How???? I know their exists split ( ) but I don't know how to use it, that's why I posted the thread.Isn't this New to Java category?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,438
    Blog Entries
    7
    Rep Power
    20

    Default

    According to the API documentation: str.split("/") does the job, i.e. it returns a String[] containing the String parts you're looking for.

    kind regards,

    Jos
    Last edited by JosAH; 10-22-2009 at 05:27 PM.

  5. #5
    nmvictor is offline Member
    Join Date
    Oct 2009
    Posts
    31
    Rep Power
    0

    Unhappy spliting the string xxx/yyyy/zzzz

    Though this forums I gathered some ideas and came up with the code below,
    Java Code:
    public class TestString {
    
    public void doit() {
     	String str = "xxx/yyyy/zzzz";
     	String [] temp = null;
     	temp = str.split("/");
     	dump(temp);
    }
    
    public void dump(String []s) {
     	for (int i = 0 ; i < s.length ; i++) {
     		System.out.println(s[i]);
            String x = s[i];
     	}
    }
    
    public static void main(String args[]) throws Exception{
      	TestString ss = new TestString();
                 ss.doit();
    }
      }
    output:
    xxx
    yyyy
    zzzz

    Now that is perfect except the string except they are printed to system.out meaning i cant do much with them other than see them, is their a way I can modify the above so that xxx, yyyy and zzz are returned as values of a separate String variable which I can use in other methods within my project.

  6. #6
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    You can pass them as a String[], which will have "xxx", "yyy", and "zzz" as elements (like you did for your dump method).

    What do you want to do with the values? Most of the time, you parse a String for a reason - this reason will tell you how to best store the three parts.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,438
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by nmvictor View Post
    Now that is perfect except the string except they are printed to system.out meaning i cant do much with them other than see them, is their a way I can modify the above so that xxx, yyyy and zzz are returned as values of a separate String variable which I can use in other methods within my project.
    The new for-loop can make it look fancy:

    Java Code:
    for (String part : "xxx/yyyy/zzzzz".split("/"))
       // do anything with the 'part' String here ...
    kind regards,

    Jos

  8. #8
    nmvictor is offline Member
    Join Date
    Oct 2009
    Posts
    31
    Rep Power
    0

    Unhappy

    Thanks to all who are helping me out, how about having these parts(
    Java Code:
    xxx, yyy, zzz
    split from
    Java Code:
     xxx/yyyy/zzzz
    assigned as values of three string variables?Actually what I want do is to send these three parts into three separate fields in a database, so of course I want to include the variables in the SQL statement and not the exact values.Any further help will be appreciated.:cool:

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,438
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by nmvictor View Post
    Thanks to all who are helping me out, how about having these parts(
    Java Code:
    xxx, yyy, zzz
    split from
    Java Code:
     xxx/yyyy/zzzz
    assigned as values of three string variables?Actually what I want do is to send these three parts into three separate fields in a database, so of course I want to include the variables in the SQL statement and not the exact values.Any further help will be appreciated.:cool:
    Read the API documentation for the PreparedStatement interface; you can 'inject' values in the String form of SQL queries without the need to fiddle with Strings yourself (and protect yourself from malicious "SQL injection" hacks).

    kind regards,

    Jos

  10. #10
    nmvictor is offline Member
    Join Date
    Oct 2009
    Posts
    31
    Rep Power
    0

    Default

    I appreciate whatever suggestions you give, what's the code anyway, to a newbie in Java,suggestions without the code don't really help much or should I start another thread on how can I "'inject' values in the String form of SQL queries without the need to fiddle with Strings"?Sorry if I'm sounding rude.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,438
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by nmvictor View Post
    I appreciate whatever suggestions you give, what's the code anyway, to a newbie in Java,suggestions without the code don't really help much or should I start another thread on how can I "'inject' values in the String form of SQL queries without the need to fiddle with Strings"?Sorry if I'm sounding rude.
    You don't learn if source code is spoonfed to you; have you read the API documentation for the PreparedStatement? I don't give you a fish, I attempt to teach you how to fish.

    kind regards,

    Jos

Similar Threads

  1. How to split a file into 2?
    By syntrax in forum New To Java
    Replies: 3
    Last Post: 09-26-2009, 06:28 AM
  2. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 08:27 PM
  3. split Keyword
    By santhoshrao in forum New To Java
    Replies: 4
    Last Post: 08-13-2008, 10:28 AM
  4. Replies: 3
    Last Post: 12-17-2007, 02:35 AM
  5. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •