Sunday, October 21, 2012

JavaScript SecondIndexOf or (x)indexOf

The JavaScript indexOf() and lastIndexOf() methods return the position of the first and last occurrence of a specified value in a string

You can learn more about them here

These can be very usefull, but what if you want the Second indexOf() to get the second occurrence of a specified value in a string. There is no standard JavaScript method for this, so I made my own one;

 function SecondIndexOf(Val, Str)  
 {  
   var Fst = Str.indexOf(Val);  
   var Snd = Str.indexOf(Val, Fst+1)  
   return Snd  
 }  

But why stop there, what if you want the third or fourth indexOf()
The following function lets you specify which occurrence of the specified value to find in the string;

 function xIndexOf(Val, Str, x)  
 {  
   if (x <= (Str.split(Val).length - 1)) {  
     Ot = Str.indexOf(Val);  
     if (x > 1) { for (var i = 1; i < x; i++) { var Ot = Str.indexOf(Val, Ot + 1) } }  
     return Ot;  
   } else { alert(Val + " Occurs less than " + x + " times"); return 0 }  
 }  

Here is an egsample of how you can use this;

   var PicPath = "/somedirectory/pics/";  
   var AppPath = picpath.substring(0, xIndexOf('/', PicPath, 2) + 1);  

Let me know if you found this usefull, or can recommend any enchancments.

Sunday, October 7, 2012

Visual Studio Tips & Tricks to Streamline Development

Here are a few of my favorite Visual Studio tips & tricks that I use to streamline my development;

Record and play temporary macro


Press Ctrl+Shift+R to record a new temporary macro, then press Ctrl+Shift+R to stop recording.
Ctrl+Shift+P will play the recorded macro.

Previous cursor positions


Ctrl+- (i.e. Ctrl and Hyphen)  cycles you through the code positions that you have visited.
Use Ctrl+Shift+- to navigate in the opposite direction.

Matching brace/comment/region/quote


Ctrl+] takes you to the matching brace. It also takes you to the matching comment, region or quote depending on where your cursor is.

Intellisense suggestions


Press Ctrl+Shift+Space to bring up the intellisense suggestions.

Line numbering


To enable/disable Line number go to Tools|Options|Text Editor|All Languages|General|Line numbers.

If you want to set this option for only one language, then choose the appropriate language instead of All Languages.

Code Snippets


There are Code Snippets for most methods that get used often. To try this, type “for”, and then hit you tab key twice. You can also do this with your “If” and “try” Statements. You can also add your own Code Snippets if there are chunks of code that you often use and don’t want to type in full each time.

Go to http://msdn.microsoft.com/en-us/library/ms165392.aspx  to learn more about this.