TechFundu

  • Increase font size
  • Default font size
  • Decrease font size
Welcome Guest,  Login | Join Now
Home Forum

Modifying a string in java..
(1 viewing) (1) Guest
Java Standard Edition development related issues discussion forum.
Go to bottomPage: 1
TOPIC: Modifying a string in java..
#54
Modifying a string in java.. 2 Years, 5 Months ago Karma: 0
Hi all,
I have a String s = "001 LEE MARCUM, 002 DEE DOUGLAS , 003 KAVI TATE";

It contanis empid and name .
i.e. 001 is empid and LEE is name.
Now i want my modified string to be "001, 002 , 003". Removing all the name values and making a string of only empid's .

How can i do this java ? Any help will be appreciated.
Thanks
roselin
Fresh Boarder
Posts: 9
graphgraph
User Offline Click here to see the profile of this user
The administrator has disabled public write access.
 
#55
Re:Modifying a string in java.. 2 Years, 5 Months ago Karma: 0
you can use split funtion of String class which take regular expression. Don't use StringTokenizer class as it is deprecated now.

public String[] split(String regex)

String s = "001 LEE MARCUM, 002 DEE DOUGLAS , 003 KAVI TATE";
1.) String [] tokens = s.split(",");

2.) Loop through all the tokens and take out first three characters using String.substring(0,3) and add them in a StringBuilder object and also apppend character (,) after that and this will give you modified string which will have employee Ids onlyl.

Please refers these api docs at sun website.

java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#split(java.lang.String)

java.sun.com/j2se/1.4.2/docs/api/java/ut...gex/Pattern.html#sum
rpawar
Fresh Boarder
Posts: 12
graphgraph
User Offline Click here to see the profile of this user
The administrator has disabled public write access.
 
#57
Re:Modifying a string in java.. 2 Years, 5 Months ago Karma: 0
Try the below code :

String arrayEmployee = "001 LEE, 002 DEE , 003 KAVI RST";
Scanner employees = new Scanner(arrayEmployee)
.useDelimiter(",");
StringBuilder str2 = new StringBuilder();
while (employees.hasNext()) {

String employee = employees.next();

String str1 = employee.substring(0,4);


str2 = str2.append(str1).append(",");


}
System.out.println("String built is ------" +str2);

}

Hope this will help you.
deepa
Fresh Boarder
Posts: 5
graphgraph
User Offline Click here to see the profile of this user
The administrator has disabled public write access.
 
#65
Re:Modifying a string in java.. 2 Years, 4 Months ago Karma: 0
the code given above will work only the emplpoyee id is of size 3
i.e. 001 002 upto 999
or aaa to zzz


if the number can be of any size... we need to know the business rule for deciding if a value is employee id or no...
rahulmaindargi
Fresh Boarder
Posts: 5
graphgraph
User Offline Click here to see the profile of this user
The administrator has disabled public write access.
 
Go to topPage: 1
get the latest posts directly to your desktop
Follow us on Twitter