Results 1 to 2 of 2
  1. #1
    itsme is offline Member
    Join Date
    Dec 2007
    Posts
    3
    Rep Power
    0

    Default splitting string and replacing

    Hi
    I am new to JAVA .I am trying to write a code where I have a string which contains several lines.The end character of each line is \n .i want to replace \n with \r\n for my purpose.I need to chk whether each line end with \r\n --if it does do nothing or else replace \n with \r\n.
    Below is teh small code i have written but its not working for me .Can any body help?
    my string name is is swift

    int length =swift.length();
    for (int i = 0; i<=length; i++)
    int value1 = swift.indexOf("\n");
    System.out.println (" value1 is " + value1 );
    int value2 = value1 -1;
    System.out.println (" value2 is " + value2 );

    char mg = swift.charAt(value2);
    char pg =swift.charAt (value1);

    System.out.println ("Its here now before conversion ");
    if ( mg == '\r' )
    { //do nothing
    System.out.println (" cntr M is there ");
    }
    else
    {

    System.out.println (" Swift message is " + swift );
    System.out.println (" Its not there " );
    swift.replace("\n","\r\n");
    System.out.println (" cntr M was not there and conversion done ");

    }

    }

  2. #2
    itsme is offline Member
    Join Date
    Dec 2007
    Posts
    3
    Rep Power
    0

    Default

    I now changed the logic.I am now taking each line in an array and checking
    whether it contains \r ( do nothing if there)or replace (\n with \r\n).
    Everything seems fine except the last replaecement .Can any body help .

    int count=0;int index=0;
    char seperator ='\n';
    do
    {
    ++ count;++ index;

    index = swift.indexOf(seperator,index);
    System.out.println (" index is " + index );
    }
    while(index!= -1);
    System.out.println (" count is " + count );

    //Extract string into array

    String[] Substr = new String[count];
    index = 0;int endIndex=0;
    for (int i=0;i< count; i++)
    {
    endIndex=swift.indexOf(seperator,index);
    if(endIndex==-1)
    Substr[i]=swift.substring(index);
    else
    {
    Substr[i]=swift.substring(index,endIndex);
    }

    if (Substr[i].contains("\r"))
    {
    System.out.println("No need of conversion");
    System.out.println("Substring" + i + "is" + Substr[i]);
    }
    else
    {
    Substr[i].replace("\n","\r\n");
    System.out.println("conversion done");
    System.out.println("Substring" + i + "is" + Substr[i]);
    }

    index=endIndex+1;
    }
    //Display
    for (int i=0;i<Substr.length;i++)
    System.out.println("Substring" + i + "is" + Substr[i]);

Similar Threads

  1. Array splitting
    By Lunarion in forum New To Java
    Replies: 3
    Last Post: 04-17-2009, 08:00 AM
  2. Replacing at an index
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-29-2008, 06:33 AM
  3. Replies: 0
    Last Post: 11-20-2007, 04:59 PM
  4. Replies: 1
    Last Post: 08-07-2007, 07:29 AM
  5. Replies: 1
    Last Post: 08-03-2007, 10:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •