Results 1 to 2 of 2
Thread: splitting string and replacing
- 12-11-2007, 01:29 PM #1
Member
- Join Date
- Dec 2007
- Posts
- 3
- Rep Power
- 0
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 ");
}
}
- 12-11-2007, 03:08 PM #2
Member
- Join Date
- Dec 2007
- Posts
- 3
- Rep Power
- 0
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
-
Array splitting
By Lunarion in forum New To JavaReplies: 3Last Post: 04-17-2009, 08:00 AM -
Replacing at an index
By bugger in forum New To JavaReplies: 2Last Post: 01-29-2008, 06:33 AM -
Using java.util.Scanner to search for a String in a String
By Java Tip in forum Java TipReplies: 0Last Post: 11-20-2007, 04:59 PM -
Help with insertName(String name) and deleteName(String name)
By trill in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:29 AM -
I can't seem to pass the value of a string variable into a string array
By mathias in forum Java AppletsReplies: 1Last Post: 08-03-2007, 10:52 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks