Thursday, October 21, 2010

Replace a part of string with another

Generally java provides a code to replace a character.This code helps you to replace a part of string with a new string in JAVA.All you need to do is pass the completeString ,oldString,new String as params to the below method.The occurrences of old string in complete string is replaced by new string .

This can be achived by string Buffer,but just i tried out with String ;)



public static String replace(String totalString,String oldString,String newString){

//offset is 0,means we are serching oldString from index 0

int offset=0;

StringBuffer temp=new StringBuffer();

try{

while(totalString.indexOf(oldString,offset)!=-1&&offset< span=""><>

//the loop iterates until index is -1

//example:totalString:text ,oldString:ex newString:ax

temp.append(totalString.substring(offset, totalString.indexOf(oldString,offset)));

//temp has value ‘t’

temp.append(newString);//value of temp is ‘tax’

offset=totalString.indexOf(oldString,offset)+oldString.length();

}

if(offset< span=""><>

temp.append(totalString.substring(offset));

//value id temp is ‘taxt’

}

}catch (Exception e) {

System.out.println("Array out of bound of exception");

}

totalString=temp.toString();

return totalString;

}

Wednesday, October 6, 2010

error 10054 during TCP read:J2me connecting to network

IoException:error 10054 during TCP read.The error ocured for me.I found that there is no proxy and authentication set in ktoolbar(C:\WTK22\bin\ktoolbar.exe) .After i set the info there is no tcp error again.


reference:http://developers.sun.com/mobility/midp/articles/wtk104/