Easy tech stuff!

Javascript Substring function

Posted: December 29th, 2008 | Author: Nischal Shetty | Filed under: JavaScript | Tags: , , , , , | No Comments »

Javascript has an inbuilt substring function which helps to extract parts of a string.

substring(start, stop) :

start (mandatory) – The starting index

stop (optional) – The index to stop extraction

Example: If you want to find the first 3 alphabets in the word INDIA, the code would be

var name = “INDIA”;

var firstThreeLetters = name.substring(0,3);

alert(firstThreeLetters);

The alert would display : IND

Note: Here start is 0 hence the extraction starts from the letter in the 0th position i.e. the first letter. The stop index is 3 which means extraction would stop at index 3 (the letter at index 3 will not be extracted)

Since stop is optional, if you only specify the start parameter, all the letters after the starting index (including the one at the start) would be extracted.

Example: Consider the same example as above, for the word INDIA if we were to give the start as 1

var name = “INDIA”;

var extractedLetters = name.substring(1);

alert(extractedLetters);

The alert would display NDIA as the output of the javascript substring method.

————————————————————————————

Indepth Analysis of Javascript substring

Let’s try and create our own Javascript substring function.

String.prototype.mySubstring = function(start,stop) {

var charArray = this.split(‘);

var temp = “”;

for(var i=start;<stop;i++)

{

temp = temp + charArray[i];

}

return temp;

}

Now, you can use this function instead of the default substring function. Include the above code in any page and you can use mySubstring inside any javascript function.

Example:

var name = “INDIA”;

var extractedLetters = name.mySubstring(0,3);

alert(extractedLetters);

The alert would display IND thus emulating the real Javascript substring function!

Note: This method is just a food for thought. To help you understand how exactly such methods would be implemented. For all practical purposes use the default substring function of Javascript.


Create a desktop shortcut to lock computer

Posted: December 10th, 2008 | Author: Nischal Shetty | Filed under: General | Tags: , , , , | No Comments »

You can lock your windows desktop by clicking on “windows key + L“. There is another way to do this. By creating a shortcut.

Right Click on your desktop and select

New -> Shortcut.

A shortcut will be created and a dialog box would pop up.

Copy paste %windir%\system32\rundll32.exe user32.dll,LockWorkStation into the text box of the dialog.

Click Next. Type any name you like.

A desktop shortcut with the name that you gave would be created. Double click anytime to lock your computer.


Generate dynamic id’s for struts html tags

Posted: September 23rd, 2008 | Author: Nischal Shetty | Filed under: Struts | Tags: , , , , | 7 Comments »

Do you use struts tags to generate html content? If yes, then sooner or later you’ll come across a scenario where you would generate html elements with the same name but would need different id’s for each element. I faced a similar predicament recently. I had no other option but to use scriptlets to generate id’s for the html elements. However, I soon found out using scriptlets within the struts html tags isn’t really straight forward.

Here’s the code I tried in my first attempt:

<%int i=0;%>

//iteration logic here

<html:text property=”example” styleid=”example<%=i%>”>

Well, if you write the code as shown above, the html code generated would be :

<input type=”text” name=”example” id = “example<%=i%>”>

and not

<input type=”text” name=”example” id=”example0″>

To get the expected result, i.e. for the scriptlet to work inside the struts html tag, write as below:

<html:text property=”example” styleid=’<%=”example”+i%>’

That’s it. If any of you ever find out a way to generate different id’s for html elements with the same name using strtus tags exclusively then do let me know :)