Results 1 to 5 of 5
-
How to split a String using split function
This tip will show the use of split function to split a String according to particular separator character.
In this program Split() function is splitting the String according to ‘:’.
Java Code:public class StringSplit { public void doit() { String str = "CompChamps:Guys:are:There"; 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]); } } } public static void main(String args[]) throws Exception{ StringSplit ss = new StringSplit(); ss.doit(); }
- 08-26-2008, 07:51 AM #2
Try this code :
public class splitdemo {
private static String REGEX = ":";
private static String INPUT = "CompChamps:Guys:are:There";
public static void main(String[] argv) {
/*A compiled representation of a regular expression.
regular expression, specified as a string, must first be compiled into an instance of this class.*/
Pattern p = Pattern.compile(REGEX);
// editing your input file, regex.txt
String[] items = p.split(INPUT);
for (int i = 0; i < items.length; i++) {
System.out.println(items[i]);
}
}
}visit : www.yoteam.co.cc
- 12-21-2008, 09:27 AM #3
Member
- Join Date
- Mar 2008
- Posts
- 10
- Rep Power
- 0
A shorter example
Java Code:public class StringSplit { public static void main(String[] argv) { System.out.println(Arrays.asList( "CompChamps:Guys:are:There".split(":"))); } }
- 02-18-2009, 08:55 AM #4
Member
- Join Date
- Feb 2009
- Location
- Delhi
- Posts
- 63
- Rep Power
- 0
to split a string use
StringTokenizer class
this will provide you a wide properties and options.
- 04-17-2009, 08:27 PM #5
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
Hello every one im new to Java and my question here is how i cant get the inistials of any word such the name John i want to get only the letter "J" will be displayed i tried to use split fanction but didnt help and this is my code :
String fname;
String mname;
String lname;
fname = JOptionPane.showInputDialog(alert, "What is Your First Name: ");
mname = JOptionPane.showInputDialog(alert, "What Is your Middle Name: ");
lname = JOptionPane.showInputDialog(alert, "What Is your Last Name: ");
JOptionPane.showMessageDialog(alert, "The full name is : " +fname.substring(0)." "+mname.substring(0)+ " "+lname.substring(0)+ " . ");
after i got the full name like John Bob, Mike
i want to initials the names so i get the first letter of each word the outpit will be J B M
I need your help
thank you
Similar Threads
-
problem with split method
By abhiN in forum New To JavaReplies: 7Last Post: 02-10-2009, 01:54 PM -
how to split a file
By nagaraaju in forum New To JavaReplies: 0Last Post: 03-14-2008, 08:45 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, 02:35 AM -
How to split a String using split function
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:32 PM -
PDF Split and Merge 0.7 beta 1
By JavaBean in forum Java SoftwareReplies: 0Last Post: 06-24-2007, 08:46 AM


LinkBack URL
About LinkBacks

Bookmarks