Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-11-2007, 03:29 PM
Member
 
Join Date: Dec 2007
Posts: 3
itsme is on a distinguished road
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 ");

}

}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-11-2007, 05:08 PM
Member
 
Join Date: Dec 2007
Posts: 3
itsme is on a distinguished road
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]);
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing at an index bugger New To Java 2 01-29-2008 08:33 AM
Array splitting Lunarion New To Java 2 12-11-2007 07:30 AM
Using java.util.Scanner to search for a String in a String Java Tip Java Tips 0 11-20-2007 06:59 PM
Help with insertName(String name) and deleteName(String name) trill New To Java 1 08-07-2007 09:29 AM
I can't seem to pass the value of a string variable into a string array mathias Java Applets 1 08-03-2007 12:52 PM


All times are GMT +3. The time now is 08:20 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org