Tuesday, July 28, 2009

Case Insensitive Replace in Java

public class TestReplaceAll {
public static void main(String[] args) {
String str = "I am a programMeR";
System.out.println("input : " + str);
str = str.replaceAll("(?i)programmer", "developer");
System.out.println("output : " + str);
}
}

This program might be helpful in replacing words which are not consistent with respect to case in a string. This piece of code was given by a colleague.

Sunday, July 26, 2009

Simple Javascript code for validating only numbers in a text box

A small script which will allow only numbers to be typed inside a textbox.
No other character or symbol will be allowed.
This would be helpful if we have a field in which only numbers should be entered by user.


function validateForNumbers(valFromTextBox)
{
if(valFromTextBox.value.length>0)
{
valFromTextBox.value =
valFromTextBox.value.replace(/[^\d]+/g, '');
}
}

Call this function on "onkeyup" event.

Bye...

Java - Rounding floating point numbers to a certain number of decimal places

Small piece of code to round decimal numbers to specified number of decimal places.
public float Round(float val, int roundOffTo){
float p = (float)Math.pow(10,roundOffTo);
val = val * p;
float tmp = Math.round(val);
return (float)tmp/p;
}

Sunday, July 19, 2009

Hi.. Started off blogging 2day..