Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Tuesday, June 25, 2013

Javascript: Function to check if the string is numeric (i.e. valid number)

Following is the javascript function which accurately validates if the supplied string a valid number or not.

function isNumber(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}



Trim, Ltrim, Rtrim, FullTrim - trimming string functions in Javascript


JQuery 1.9+ have inbuilt functions for string trimming, but they aren't supported in many browsers, such as, IE 8, IE 9, etc., and so I refrain myself from using those function, and hence there is a need to have our custom function to perform this job!

Following are javascript functions for trimming strings: 

function StringTrim(inputText){return inputText.replace(/^\s+|\s+$/g, '');};

function StringLtrim(inputText){return inputText.replace(/^\s+/,'');};

function StringRtrim(inputText){return inputText.replace(/\s+$/,'');};

function StringFulltrim(inputText){return inputText.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,'').replace(/\s+/g,' ');};

Add them in javascript library of your application, and you can use them to trim strings in various ways, such as, left trim, right trim, trim, and full trim.

Hope this helps!

Friday, May 10, 2013

How to convert an array of one type to another type in using Linq in .NET

Following is the sample code that converts a string array into GUID array with faster performance:

String strGUIDs[];
//Consider the above array is containing all GUIDs in string format.
Guid[] guids = Array.ConvertAll(strGUIDs, x => Guid.Parse(x));

You can convert an array from one type to another using the above approach.