31
Jan 08

Exchange the value between two variables without using a temporary variable

Let me show you how you can interchange the values stored in two variables without using a temporary variable.

Case 1: For two integers

int a = 10;

int b = 20;

a = a+b;

b = a-b;

a = a-b;

Thats all!!! When you print the values stored in these two variables, you’ll notice that the values have been interchanged!

Case 2 : For two Strings

String s1 = “Shetty”;
String s2 = “Nischal”;

s1 = s1+s2;
s2 = s1.substring(0,s1.length()-s2.length());
s1 = s1.substring(s2.length());

System.out.println(“My name is “+s1+” “+s2);

The output will be: My name is Nischal Shetty


25
Jan 08

Call Javascript function on pressing ‘Enter’ key

In most of the search pages you might have noticed how you can type your queries into the search box and search by pressing the enter key instead of having to click the ‘Search’ button present in the page.

Wondering how to do it? Well, wait no more. Implement the same feature in your pages too.

All we need to know is the key code for the enter key which happens to be ’13′.

Then under the event onKeyDown we can call the javascript which submits the page.

Go through the example below and you’ll know it all.

{

<input type=”text” onKeydown=”Javascript: if (event.keyCode==13) Search();”>

<input type=”button” value=”Search” onClick=”Search();”>

}

The above code will display a text box followed by a button labelled Search as shown in the figure below:(Note: Click on the thumbnail to view)

Search On enter key press

You’ll notice that the onClick event of the Search button calls a javascript function named Search(). To have the same function called on pressing the enter key when the cursor is in the text box, we check if the keycode is 13 and subsequently call the Search() function.


17
Jan 08

Wildcards(“_” and “%”) in SQL

We can use the WILDCARDS “_” and “%” in our SQL queries be it MS SQL or My SQl as follows:

Wildcard underscore “_” :

“_” helps to match exactly one character.

Ex. select name from name_table where name like “c_p”

The above query will return words such as “cap”, “cop”, “cup” or basically anything which is a three letter word starting with the letter “c” and ending with “p”.

Wildcard “%” :

“%” helps match any number of characters. Let’s take the same example as above:

select name from name_table where name like “c%p”

The output can be “cap”, “cop” and can also be “coooop”, “caawjshygfvsbchjsp” etc etc.

So what basically happens with “%” is that it will include any number of characters whereas “_” will include only one character.