An array of C#, PHP, and HTML programming articles, tutorials, and resources

Posts Tagged ‘ string ’

One of my favorite and most heavily used PHP common functions retrieves variables from the query string. I found that without this function, I was inefficiently checking if the variable existed, and if not, I was then was manually setting a default value. The function takes in two string variables that handle the above problem. $urlStringName is the name of the variable as stated in the query string and $returnIfNotSet is the string that will be returned if the $urlStringName is not found. Check out the code snippet below:

function getUrlStringValue($urlStringName, $returnIfNotSet) {
  if(isset($_GET[$urlStringName]) && $_GET[$urlStringName] != "")
    return $_GET[$urlStringName];
  else
    return $returnIfNotSet;
}

Let’s run through an example of how this function works. Assume we have the following url:

http://www.victorchen.info/index.php?firstName=Victor&lastName=Chen

If I am looking to find the string value of the first and last name, but return john or doe if the query variable firstName or lastName is not found, respectively. I would write the following:

$firstName = getUrlStringValue("firstName", "john");
$lastName = getUrlStringValue("lastName", "doe");

In this example, we would expect to store $firstName to be “Victor” and $lastName to be “Chen”.

However, if the url arrives as (missing the lastName query string variable):

http://www.victorchen.info/index.php?firstName=Victor

We can expect to see $firstName as “Victor” and $lastName as “Doe”.

Switch Statement in C#

by Victor | July 3, 2008 in C# | 3 Comments

When possible, a programmer should choose to use a switch statement over an if statement. Note that it is not always possible to replace an if statement with a switch statement. The right time to use a switch statement is when the if statement is constantly comparing the same variable. In our example, we are constantly comparing the text in the Label control lblDayOfWeek. To make it easy, I have made blue the string text that will be used in our comparison.

switch (lblDayOfWeek.Text)
{
   case "1":
      lblDayOfWeek.Text = "Sunday";
      break;
   case "2":
      lblDayOfWeek.Text = "Monday";
      break;
   case "3":
      lblDayOfWeek.Text = "Tuesday";
      break;
   case "4":
      lblDayOfWeek.Text = "Wednesday";
      break;
   case "5":
      lblDayOfWeek.Text = "Thursday";
      break;
   case "6":
      lblDayOfWeek.Text = "Friday";
      break;
   case "7":
      lblDayOfWeek.Text = "Saturday";
      break;
   default:
      lblDayOfWeek.Text = "";
      break;
}

The final default is equivalent to else, which means if none of the cases match, the default route will be taken. The greatest advantage to using the switch statement is efficiency. When a swtich statement is called, only one comparison is made.

Note that the switch statement is not restricted to string or integer. It is also possible to use enum or virtually any other comparable variable.

The integer data type is a more than sufficient way to represent whole numbers. But what happens when you need all your numbers to display as 16 digits, with the number left padded with zeros? The integer data type in C# is unable to handle displaying a whole number left padded with zeros.

PadLeft(int totalWidth, char paddingChar)

The key to this is that you will need to first convert the integer to a string, and then apply the PadLeft function. Below is our example of converting an three digit integer into a sixteen digit string consisting of only numbers.

int myNumber = 234;
string myStringNumber = myNumber.ToString().PadLeft(16, '0');

Note that this quick easy method will that trouble handling negative numbers. But with a couple of simple checks, it should be relatively easy to handle that case. Also note that this PadLeft is not restricted to numbers, but can also be used to insert additional letters or symbols in regular string literals.