How to disable Block on single Line in Visual Studio 2015.

When you create an auto property in Visual Studio 2015 , you will notice that it would be in a single line as shown below.

public int Name { get; set; }

When you use the shortcut for formatting in Visual Studio 2015 (Ctrl + K and Ctrl +D) , you will notice that the formatting would stay like the one shown in the above code.

There are times when you might want to have this formatting in multi-line instead of a single line. (I would prefer single line though :)).

How to Disable Block on single Line in Visual Studio 2015 ?

To change this behaviour , follow the below steps.

1. Launch Visual Studio 2015 and navigate to Tools -> Options.

2. In the Options Dialog left sidebar , select Text editor -> C# -> Formatting -> Wrapping and then uncheck the option “Leave block on single line”.

3. Click ok button and now apply the formatting. You should see the auto property in multi-line as shown below.

public string Name
{
    get; set;
}

Read more

Filter an array in JavaScript

You might want to filter an array in JavaScript by passing the filter criteria and return the filtered array. In this scenario , you can use the Array’s filter method as shown below.

How to Filter an array in JavaScript ?

var actors = new Array('vijay', 'ajith', 'surya', 'arya');
var retArray = actors.filter(function(item) {
    return item != 'arya';
});
console.log(retArray);

Read more

Convert Decimal to Octal , Hexadecimal and Binary Value in JavaScript

If you have a decimal number in JavaScript and want to convert it to its equivalent octal , hexadecimal and binary value , you can use the toString method by passing the right radix as parameter.

How to Convert Decimal number to Octal , Hexadecimal and Binary in JavaScript ?

The numbers in JavaScript are by default of base 10. The Hexadecimal numbers usually begins with 0x and octal number begins with 0.

To convert a decimal value to octal , pass the value 16 as parameter to the toString method of the number. Similarly , pass the value 8 and 2 as parameter to the toString method respectively to convert it to octal and binary format.

<script type="text/javascript">
    var decimalNumber = 10;
    console.log("Decimal Number is " + decimalNumber);

    var octalNumber = decimalNumber.toString(8);
    console.log("Octal Number is " + octalNumber);

    var hexadecimalNumber = decimalNumber.toString(16);
    console.log("Hexadecimal Number is " + hexadecimalNumber);

    var binaryNumber = decimalNumber.toString(2);
    console.log("Binary Number is " + binaryNumber);
</script>

Read more

Replace HTML tags with named entities in JavaScript

There are times when you might want to paste the code snippet in to a webpage. Imagine if that code snippet contains the HTML tags and somehow you want to escape the markup code without the browser parsing it. An idea for this would be to use convert the angle brackets to its equivalent named entities like &lt; and &gt;.

You can use the combination of the string’s replace method and regular expression as shown below. Just use the global flag in the regular expression so that all the instances of the angle brackets are found and replaced with their named entities.

How to replace HTML tags with named entities in JavaScript ?

<script type="text/javascript">
    var inputStr = "<pre>This blog post is written by <h1> Senthil </h1></pre>";
    inputStr = inputStr.replace(/</g, "&lt;");
    inputStr = inputStr.replace(/>/g, "&gt;");
    console.log(inputStr);
</script>

Read more

Replace all occurrences of a string in JavaScript ?

You might want to replace all the occurrences of the matched substrings with the new string in JavaScript.

You can use the combination of regular expression with the replace method defined in the String type to do it.

How to replace all the occurrences of a string in JavaScript ?

var inputStr = "Senthil is a Windows Phone Developer and Windows Phone enthusiast";
var regularexpress = new RegExp('/Windows Phone', "g");
var newStr = inputStr.replace(regularexpress, "Mobile");
console.log(newStr); 

Read more

Insert special character into a string in JavaScript

If you want to insert a special character into a string in JavaScript , you can use he escape sequences to do it.

For example , if you want to insert the copyright symbol into a string in JavaScript , you can use the \u00A9 escape sequence as shown below.

How to Insert Special Character in to a string in JavaScript ?

<script>
    var input = "This page is \u00A9 Senthil Kumar";
    document.write(input);     
</script>

Read more