Wednesday, 15 July 2015

How to remove string characters except first in java -


In Java, I have the string:

  James Me, Jack Jones  

I want to skip the characters except the first and add the dot retrun to which should look like J. Jones

How do I do this?

I have tried to do this

 for  (int i = 1; i & lt; str.length (); i ++) {if (i % 2 == 0) {dot = str.replaceAll ("[az]", "."); JTextArea2.setText (dot); }}  

but instead of all the lower case charatcers

You can optionally use it, which works like you

  string A = "James May"; String [] b = a.split (""); String C = B [0] .charAt (0) + "."; For (int i = 1; i & lt; b.length; i ++) {c + = b [i] + ""; } System.out.println (c);  

Or if you want something that uses it many times: (credit to Avinash Raj)

  string A = "James May, Jack Jones , Jame Martinez "; String [] b = asptt (","); String c = b [0]. ReplaceFirst ("(? & Lt; = ^.) \\ S +", "."); For (int i = 1; i & lt; b.length; i ++) {c + = "," + b [i] .replaceFirst ("(? & Lt; = ^.) \\ S +" , "."); } System.out.println (c);  

No comments:

Post a Comment