5 Replies Latest reply: Jul 11, 2007 4:13 PM by 807605 RSS

    How can I make the substring stays at the same location after modified?

    807605
      I wrote this program basically to format the name to have LN, FN and SSN. This program works but the result was not as expected.
      I want the SSN stays at the same position:
      Chang, Norma .............(space).....................234-22-3321
      Can anyone help me?.
      import java.util.*;
      
      public class test
      {
           public static void main(String args[])
           {
                String s = "norma,  chang      23--422--3-3-2-1";
                String n     = formatNm(s);
                String f = formatSSN(s);
                System.out.print(n+f);
           }
      /**************************************************************/      
           private static String formatNm(String s)  
           {
                String lastN,firstN;
                
                //for position of the comma
                int comma       =     s.indexOf(',');   
                firstN          =     s.substring(0,comma);
                lastN               =     s.substring(comma+1,18);
                firstN          =     firstN.trim();
                lastN               =     lastN.trim();
                firstN          =     capitalize(firstN);     
                lastN               =     capitalize(lastN);
                s                    =     (lastN + ", " + firstN);     
                return (s);          
           }     
      /*****************capitalize()********************************/
      //capitalize function will cap the first letter of name
           private static String capitalize(String s)  
           {
                String firstChar,rest;
                firstChar      = s.substring(0,1);
                rest                = s.substring(1);
                firstChar     = firstChar.toUpperCase();
                rest               = rest.toLowerCase();
                s                    = firstChar + rest;
                return(s);
           }
      /************************************************************/
           private static String formatSSN(String s)
           {
                String f = s.substring(19);
                String num = f.replace("-","");
                          
                StringBuilder sb = new StringBuilder(num);
                sb.insert(3, '-');
                sb.insert(6, '-');
                return sb.toString();
           }
                
      }     
      Message was edited by:
      aivon1st

      Message was edited by:
      aivon1st