Results 1 to 11 of 11
- 10-22-2009, 05:38 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 31
- Rep Power
- 0
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( )
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); }
Java Code:split( )
- 10-22-2009, 05:49 PM #2
Yep, use String.split(regex)
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 10-22-2009, 05:58 PM #3
Member
- Join Date
- Oct 2009
- Posts
- 31
- Rep Power
- 0
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?
- 10-22-2009, 06:00 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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,
JosLast edited by JosAH; 10-22-2009 at 06:27 PM.
- 10-22-2009, 06:14 PM #5
Member
- Join Date
- Oct 2009
- Posts
- 31
- Rep Power
- 0
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(); } }
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.
- 10-22-2009, 06:23 PM #6
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
- 10-22-2009, 07:35 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 10-23-2009, 09:48 AM #8
Member
- Join Date
- Oct 2009
- Posts
- 31
- Rep Power
- 0
Thanks to all who are helping me out, how about having these parts(
Java Code:xxx, yyy, zzz
Java Code:xxx/yyyy/zzzz
- 10-23-2009, 09:59 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 10-23-2009, 10:31 AM #10
Member
- Join Date
- Oct 2009
- Posts
- 31
- Rep Power
- 0
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.
- 10-23-2009, 10:47 AM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Similar Threads
-
How to split a file into 2?
By syntrax in forum New To JavaReplies: 3Last Post: 09-26-2009, 07:28 AM -
How to split a String using split function
By Java Tip in forum java.langReplies: 4Last Post: 04-17-2009, 09:27 PM -
split Keyword
By santhoshrao in forum New To JavaReplies: 4Last Post: 08-13-2008, 11:28 AM -
How to split a string into multiple lines of x characters each
By JackJ in forum New To JavaReplies: 3Last Post: 12-17-2007, 03:35 AM -
How to split a String using split function
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 10:32 PM
Bookmarks