How to Convert a string into Title Case in .Net C#

Solution:

LabelName.Text = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase((“amit KUMAR”).ToLower());

Result:

Amit Kumar

Advertisements

How to add leading zeros to a number

I saw the question “How to add leading zeros to a number” on the ASP.NET forums countless times before. And often the answers provide solutions that work, but are overkill as well.

The two cleanest methods I know are String.Format and PadLeft.

   1:  Console.WriteLine("Using .ToString()");
   2:  Console.WriteLine(String.Format("{0:0000}", 16));
   3:   
   4:  Console.WriteLine("-------------------");
   5:   
   6:  Console.WriteLine("Using .PadLeft()");
   7:  Console.WriteLine(Convert.ToString(16).PadLeft(4, '0'));

You can see the result here.

//Using .ToString()
//0016
//-------------------
//Using .PadLeft()
//0016

How to add google map to your website.

Marker

To add the Google map on your asp.net web page. Simply paste the code given below.

<script src=”http://maps.google.com/maps?file=api&amp;v=2&amp;key=AIzaSyBVaXuUwG51lC0vu6q-E-PzaCGI_u6UX1A&sensor=false&#8221;
type=”text/javascript”></script>
<script type=”text/javascript”>

function initialize() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById(“map_canvas”));
map.setCenter(new GLatLng(51.659369, -0.029843), 15);
map.setUIToDefault();
marker = new GMarker(new GLatLng(51.659369, -0.029843));
map.addOverlay(marker);
}

}

</script>

Insert a div named “map_canvas” where you want to put map on your page.

<div id=”map_canvas” style=”width: 300px; height: 350px”></div>

For showing any address just pass the longitude and latitude values for your address and replace:
map.setCenter(new GLatLng(51.659369, -0.029843), 15);

Get longitute and latitute from address for google map

Note: you also need to change the Key for your project. You can generate new key from: https://code.google.com/apis/console/

Get Google Map API Key for website

Get File Name From a Path

Hi All,
while working on File System, Sometime we need to extract the file name from a given path. In dot Net it is very simple task as Framework library provide a specific class for doing these kind of task. “Path” is the class under the System.IO namespace has various methods for working on path.
GetFileName is the method of this class that extract the File name from the path.

[Visual Basic]
Dim fileName As String = “C:\mydir\myfile.ext”
Dim pathname As String = “C:\mydir\”
Dim result As String
result = Path.GetFileName(fileName)
Console.WriteLine(“GetFileName(‘{0}’) returns ‘{1}'”, fileName, result)
result = Path.GetFileName(pathname)
Console.WriteLine(“GetFileName(‘{0}’) returns ‘{1}'”, pathname, result)

[C#]
string fileName = @”C:\mydir\myfile.ext”;
string path = @”C:\mydir\”;
string result;
result = Path.GetFileName(fileName);
Console.WriteLine(“GetFileName(‘{0}’) returns ‘{1}'”,
fileName, result);
result = Path.GetFileName(path);
Console.WriteLine(“GetFileName(‘{0}’) returns ‘{1}'”,
path, result);