Bits & Bytes

Posts Tagged ‘string’

Printing Messages to the Console Window in C#

This is one of the most fundamental elements of C# programming. In a simple console application, we often want to output some value or set of values to the console window where we can see them. To do this, we can call the functions Console.Write() and Console.WriteLine(). Both of these functions can be use with all of the primitive data types, including strings. The main difference between the functions is that the second one puts an endline or linefeed/carriage return at the end of the output to set the future output to the next line.

To demonstrate, we have a simple program below that prints the closing prices on a given day for two company stock symbols: MSFT and RSH. These two symbols, MSFT and RSH, are the ticker symbols for the companies Microsoft and Radio Shack. This is a simple Console Application project. All of the code outside of the Main() function is standard, boilerplate code that is generated by the Visual Studio IDE. So, we will ignore that and talk about the output statements inside the Main() function.

Inside the Main() function, I start with four lines that output message “Closing Prices – Date: 8/21/2014” with a line underneath it. Notice that I used the Console.Write() function for the first two print statements so that all of the text from the first three statements was printed on one line. The next call to Console.WriteLiine() adds the endline to separate the output for the next line of dashes. The line of dashes is written with the Console.Write() function. However, I added the “\u000D\u000A” at the end which is the equivalent of a linefeed/carriage return or endline character. This makes the call the equivalent of a call to Console.WriteLine(). It is useful to know this, since this can be used anywhere inside of a string to create an endline.

In the last four lines, I output the symbols for Microsoft and Radio Shack along with their respective closing prices for the day. To do this, I created decimal type variables and assigned them values. The decimal data type requires that I put an “M” suffix at the end of each number so that the compiler interprets it as a decimal type. Then I call Console.WriteLine() twice for each string that is passed in. The first string is created automatically from the string literal, “MSFT: ” concatenated with the decimal value contained in dMsftPrice via the “+” operator. The final output look like this:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyConsoleApplication {
    class Program {
        static void Main(string[] args) {
            Console.Write("Closing Prices - ");
            Console.Write("Date: ");
            Console.WriteLine(new DateTime(2014, 8, 21).ToShortDateString());

            decimal dMsftPrice = 45.22M;
            Console.WriteLine("MSFT: " + dMsftPrice);
            decimal dRshPrice = .67M;
            Console.WriteLine("RSH:   " + dRshPrice);

PHP: How to Find a Substring in a String

How do you find a substring within a string? In PHP, the answer is not quite as straightforward as it should be. But, it’s still easy to do–you just need to be aware of a couple things.

Suppose we have the string “ABCDEFG”. There are two cases we want to look at: when we want to find “DEF” in the middle of the string, and when we want to find “ABC” at the beginning.

The function to find a substring is called strpos(). It can take up to three parameters:

strpos($haystack, $needle, $offset);

  1. $haystack: (required) the full string we want to search within
  2. $needle: (required) the substring we are searching for
  3. $offset: (optional) if we want the function to start looking for the substring at a position which is not at the beginning of the $haystack

When we call strpos() on the string, it will return the position of the substring as an integer (starting from 0), if it finds it. If it doesn’t find the substring, it will return false (which is 0 in PHP). So, when we search for “DEF”, the function will return 3 which is the position of the “D” (base 0). However, when we search for “ABC”, strpos() will return 0 because it found the substring at position 0.

So, we have to construct our code in this way to check the return value from the function:

$ret = strpos("ABCDEFG", "ABC");

if($ret === false) {
    echo("We did not find the substring.");
} else {
    echo("We found the substring!");

Here are some sample cases:

strpos(“ABCDEFG”, “ABC”); –> returns integer 0, substring found

strpos(“ABCDEFG”, “DEF”); –> returns integer 3, substring found

strpos(“ABCDEFG”, “XYZ”); –> returns a 0 = to PHP’s false keyword. substring not found

In the above code, the operator === checks whether two values are identical, which means whether the two values are of the same PHP type. The strpos() function will return a 0 that is equivalent to the false keyword in PHP if it does not find the substring. It will return an integer 0 if it finds the substring starting at position 0.  So, we use the operator which checks whether the two values are identical, and if a 0 was returned, whether the function meant that 0 to be false or to be the position of the substring it found.