Home

Data Types: Strings

 

Strings Fundamentals

 

Introduction

A string is a group of characters or symbols considered as one entity. To initialize a string, include its value in double-quotes. Here is an example:

let message = "Welcome to the wonderful world of functional programming!"

To display a string, you can assign it to the Text property of a label or any control that can hold text. Here is an example:

open System
open System.Windows.Forms

let exercise : Form = new Form()
exercise.Text <- "Exercise"
exercise.Width <- 350

let lblMessage = new Label()

lblMessage.Left <- 12
lblMessage.Top <- 18
lblMessage.Width <- 328

lblMessage.Text <- "Welcome to the wonderful world of functional programming!"

exercise.Controls.Add lblMessage

Application.Run exercise

This would produce:

Introduction to Strings

You can also first declare a variable and initialize it with a string, then assign that string to the Text property of the control. Here is an example:

open System
open System.Windows.Forms

let exercise : Form = new Form()
exercise.Text  <- "Exercise"
exercise.Width <- 350

let lblMessage = new Label()

lblMessage.Left  <- 12
lblMessage.Top   <- 18
lblMessage.Width <- 328

let message = "Welcome to the wonderful world of functional programming!"
lblMessage.Text <- message

exercise.Controls.Add lblMessage

Application.Run exercise

A string is represented by a data type named string. As mentioned in Lesson 2, when declaring a variable, if you want to indicate that it is a string, type a colon after its name and use the string data type. Here is an example:

open System
open System.Windows.Forms

let exercise : Form = new Form()
exercise.Text <- "Exercise"
exercise.Width <- 350

let lblMessage = new Label()

lblMessage.Left <- 12
lblMessage.Top <- 18
lblMessage.Width <- 328

let message : string = "Welcome to the wonderful world of functional programming!"
lblMessage.Text <- message

exercise.Controls.Add lblMessage

Application.Run exercise

When declaring a string variable, you can initialize it with an empty space, a character, a symbol, a word, or a group of words. The value given to a string must be included in double-quotes.

If you have a long string that you want to span on more than one line, cut its sections with a back slash each (although the second line in the following example is indented, you don't have to indent). Here is an example:

open System
open System.Windows.Forms

let exercise : Form = new Form()
exercise.Text <- "Exercise"
exercise.Width <- 350

let lblMessage = new Label()

lblMessage.Left <- 12
lblMessage.Top <- 18
lblMessage.Width <- 328

let message : string = "Welcome to the wonderful world \
			of functional programming!"
lblMessage.Text <- message

exercise.Controls.Add lblMessage

Application.Run exercise

There are many other issues related to strings. We cannot review them now.

Concatenating Strings

String concatenation consists of adding one string to another. This is done using the addition operation. Here is an example:

let userName = "pjacobson"
let emailDomain = "@yonson.com"
let emailAddress = userName + emailDomain

In the same way, you can use as many addition operators as necessary to concatenatte strings. Here is an example:

open System
open System.Windows.Forms

let firstName = "Philippe"
let lastName = "Jacobson"
let fullName = firstName + " " + lastName

let exercise = new Form()
exercise.Text <- "Exercise"

let lblFirstName = new Label()
lblFirstName.Left <- 12
lblFirstName.Top <- 21
lblFirstName.Text <- "First Name:"
lblFirstName.Width <- 64
exercise.Controls.Add lblFirstName

let txtFirstName = new TextBox()
txtFirstName.Left <- 82
txtFirstName.Top <- 18
txtFirstName.Width <- 88
txtFirstName.Text <- firstName
exercise.Controls.Add txtFirstName

let lblLastName = new Label()
lblLastName.Left <- 12
lblLastName.Top <- 48
lblLastName.Text <- "Last Name:"
lblLastName.Width <- 64
exercise.Controls.Add lblLastName

let txtLastName = new TextBox()
txtLastName.Left <- 82
txtLastName.Top <- 45
txtLastName.Width <- 88
txtLastName.Text <- lastName
exercise.Controls.Add txtLastName

let lblFullName = new Label()
lblFullName.Left <- 12
lblFullName.Top <- 74
lblFullName.Text <- "Full Name:"
lblFullName.Width <- 64
exercise.Controls.Add lblFullName

let txtFullName = new TextBox()
txtFullName.Left <- 82
txtFullName.Top <- 72
txtFullName.Width <- 128
txtFullName.Text <- fullName
exercise.Controls.Add txtFullName

Application.Run exercise

This would produce:

Strings Concatenation

Formating a String

Formatting a string consists of creating a string that includes placeholders for special characters. Those special characters would be replaced by appropriate values. The built-in function to do this is called sprintf. The formula to follow is:

sprintf "%special-character . . ." value(s)

After the sprintf name, add a pair of double-quotes of a string. Inside those quotes, you can add words. Whereever you want to use a special value, place the % sign followed by a special character. We will see what characters can be used and why. You can continue adding other words or characters inside the double-quotes to create a complete sentence if necessary.

Outside the quotes, add a value for each % placeholder. The order of the values must correspond to the other of the % placeholders.

At the end, the sprintf function returns a string. As a result, you can assign a call to sprintf to a variable, or you can call it directly where it is needed.

The letter used for strings is s. Here is an example:

open System
open System.Windows.Forms

let exercise = new Form()
exercise.Text <- "Exercise"
exercise.Width <- 350

let lblMessage = new Label()

lblMessage.Left <- 12
lblMessage.Top <- 18
lblMessage.Width <- 328

let name = "James"

let welcome = sprintf "Hi %s, welcome to F#" name

lblMessage.Text <- welcome

exercise.Controls.Add(lblMessage)

Application.Run(exercise)

This would produce:

Introduction to Strings

Converting a Value to a String

To let you convert any value to a string, the F# language provides a built-in function named string. Here is an example of calling it:

let number = 138
let result = string number

A Verbatim String

In some cases, you will want to treat an escape sequence in a particular way, or to simply ignore it. In other words, you may want the compiler to ignore that a combination of \ and a character is an escape sequence. A typical example is in specifying the path to a file in the computer. In this case, you must create a verbatim string.

To create a verbatim string, start it with the @ sign outside the opning double-quote. Here is an example:

let fileName = @"G:\Corporate Presentations\References\Introductory Material.docx"

If you precede a string value with the @ sign, all escape sequences in the string would be ignored, except for the double-quotes. If you want the double-quotes to be ignored, start and end the string with three double-quotes. Here is an example:

let fileName = @"@"@"G:\Corporate Presentations\References\Introductory Material.docx"@"@"

The Length of a String

The length of a string, also referred to as its size, is the number of characters it contains. To let you get this information, The String class is equipped with a property named Length. Here is an example:

open System
open System.Windows.Forms

let exercise = new Form(Width = 355, Text = "Create Passrod")

// New Password
exercise.Controls.Add(new Label(Left = 12, Top = 22, Width = 85, Text = "New Password:"))
let txtNewPassword = new TextBox(Left = 112, Top = 19, Width = 124)
exercise.Controls.Add txtNewPassword

// Password Length
let txtPasswordLength = new TextBox(Left = 242, Top = 19, Width = 19)
exercise.Controls.Add txtPasswordLength
exercise.Controls.Add(new Label(Left = 267, Top = 22, Width = 80, Text = "Characters"))

let txtNewPasswordTextChanged e =
    let length = txtNewPassword.Text.Length

    txtPasswordLength.Text <- string length
txtNewPassword.TextChanged.Add txtNewPasswordTextChanged  

do Application.Run exercise

You can access the Length property when processing the individual characters of a string.

 

Having Characters

 

Introduction

To support letters, characters, and other types of symbols from the alphabet or out of the alphabet, the F# language provides the char data type. The char data type is identified in the .NET Framework by the Char structure, which gets represented with a 16-bit value. As a result, the F#'s char is just a customized name of the .NET's Char structure.

As seen in Lesson 4, to declare a variable that can hold one character, a letter, or a symbol, when initializing the variable, include its value between two single-quotes. Here are examples:

open System
open System.Windows.Forms

let gender = 'm'
let moneySymbol = '$'
let multiplication = '*'
let numberOne = '1'

Categories of Characters

As far as computers or operating systems are concerned, every readable or non-readable symbol used in an application is a character. All those symbols are considered objects of type char. The Char structure is able to recognize every one of them. In fact, the Char structure considers the symbols into various categories:

Letters: An alphabetical letter is a readable character recognized by a human language. To let you find out whether a character is a letter, the Char structure is equipped with a static method named IsLetter. It is overloaded with two versions.

Digits: A digit is a symbol used in a number. It can be 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9. To let you find out whether a character is a digit, the Char structure is equipped with the IsDigit() static method that is overloaded with two versions. In the same way, the Char structure provides various methods to test the category of characters being used. All these methods are static and they are given in two versions. Each has a version that takes one argument as a character. If the argument is the type sought, the method returns true. Otherwise it returns false. The methods are:

Method Returns true if the argument is
IsLetter (c : char) : bool A letter
IsLower (c : char) : bool A lowercase letter
IsUpper (c : char) : bool An uppercase letter
IsDigit (c : char) : bool A digit
IsNumber (c : char) : bool A digit or any other type of number
IsLetterOrDigit (c : char) : bool A letter or a digit
IsControl (c : char) : bool A control character (Ctrl, Shift, Enter, Del, Ins, etc)
IsPunctuation (c : char) : bool A punctuation such as , . - ! ? ' " ( ) | # \ / % & * > @ < � �
IsSymbol (c : char) : bool A symbol such as | + � � � � = ^ � $
IsWhiteSpace (c : char) : bool An empty space such as created by pressing the SPACE bar
IsSeparator (c : char) : bool An empty space or the end of a line

Here are examples:

open System
open System.Windows.Forms

let exercise = new Form(Width = 385, Height = 170, Text = "Create Password")

// New Password
exercise.Controls.Add(new Label(Left = 12, Top = 22, Width = 85, Text = "New Password:"))
let txtNewPassword = new TextBox(Left = 112, Top = 19, Width = 124)
exercise.Controls.Add txtNewPassword

// Password Length
let txtPasswordLength = new TextBox(Left = 242, Top = 19, Width = 20)
exercise.Controls.Add txtPasswordLength
exercise.Controls.Add(new Label(Left = 267, Top = 22, Width = 80, Text = "Characters"))

// Confirm Password
exercise.Controls.Add(new Label(Left = 12, Top = 51, Width = 100, Text = "Confirm Password:"))
let txtConfirmPassword = new TextBox(Left = 112, Top = 48, Width = 124)
exercise.Controls.Add txtConfirmPassword

// Lowercase Letters
let txtLowercaseLetters = new TextBox(Left = 112, Top = 78, Width = 20)
exercise.Controls.Add txtLowercaseLetters
exercise.Controls.Add(new Label(Left = 136, Top = 81, Width = 100, Text = "Lowercase Letters"))

// Uppercase Letters
let txtUppercaseLetters = new TextBox(Left = 242, Top = 78, Width = 20)
exercise.Controls.Add txtUppercaseLetters
exercise.Controls.Add(new Label(Left = 266, Top = 81, Width = 100, Text = "Uppercase Letters"))

// Digits
let txtDigits = new TextBox(Left = 112, Top = 108, Width = 20)
exercise.Controls.Add txtDigits
exercise.Controls.Add(new Label(Left = 136, Top = 111, Width = 100, Text = "Digits"))

// Symbols
let txtSymbols = new TextBox(Left = 242, Top = 111, Width = 20)
exercise.Controls.Add txtSymbols
exercise.Controls.Add(new Label(Left = 266, Top = 114, Width = 100, Text = "Symbols"))


let txtNewPasswordTextChanged e =
    let mutable digits = 0
    let mutable symbols = 0
    let mutable lowercaseLetters = 0
    let mutable uppercaseLetters = 0
    let length = txtNewPassword.Text.Length

    for i = 0 to length - 1 do
        if Char.IsDigit(txtNewPassword.Text.[i]) then
            digits <- digits + 1
        if if Char.IsSymbol(txtNewPassword.Text.[i]) || Char.IsPunctuation(txtNewPassword.Text.[i]) then
            symbols <- symbols + 1

    txtPasswordLength.Text <- string length
txtNewPassword.TextChanged.Add txtNewPasswordTextChanged  

do Application.Run exercise

The String as a Group of Characters

   

Introduction

A string is a group of characters. After declaring and initializing a string variable, it is considered a collection of characters where each symbol occupies a specific position. The positions are numbered so that the most left character of the string occupies index 0; the second character is at index 1, and so on.

To support this idea of a collection of characters, the String class is equipped with a property named Chars. This is also how you can retrieve the character at a specific index in the string, using the .[] operator.

The Type of Character in a String

As mentioned already, the Char structure is equipped to find out what type of character is used somewhere. In fact, you can scan a string to find out what type of character is used in a certain position within the string. To support this, the Char structure has various methods that are second versions to the methods we saw for characters passed as arguments. Each of these second versions takes two arguments. The first argument is passed as a string. The second argument is the index where the character is positioned. If the character at that position is the category sought, the method returns true. Otherwise it returns false. The methods are:

Method Returns true if the character at index i within string s is
IsLetter (s : string) (i : int) : bool A letter
IsLower (s : string) (i : int) : bool A lowercase letter
IsUpper (s : string) (i : int) : bool An uppercase letter
IsDigit (s : string) (i : int) : bool A digit
IsNumber (s : string) (i : int) : bool A digit or any other type of number
IsLetterOrDigit (s : string) (i : int) : bool A letter or a digit
IsControl (s : string) (i : int) : bool A control character (Ctrl, Shift, Enter, Del, Ins, etc)
IsPunctuation (s : string) (i : int) : bool A punctuation such as , . - ! ? ' " ( ) | # \ / % & * > @ < � �
IsSymbol (s : string) (i : int) : bool A symbol such as | + � � � � = ^ � $
IsWhiteSpace (s : string) (i : int) : bool An empty space such as created by pressing the SPACE bar
IsSeparator (s : string) (i : int) : bool An empty space or the end of a line

Here are examples:

let txtNewPasswordTextChanged e =
    let mutable digits = 0
    let mutable symbols = 0
    let mutable lowercaseLetters = 0
    let mutable uppercaseLetters = 0
    let length = txtNewPassword.Text.Length

    for i = 0 to length - 1 do
        if Char.IsDigit(txtNewPassword.Text.[i]) then
            digits <- digits + 1
        if Char.IsSymbol(txtNewPassword.Text.[i]) || Char.IsPunctuation(txtNewPassword.Text.[i]) then
            symbols <- symbols + 1
        if Char.IsLower(txtNewPassword.Text, i) then
            lowercaseLetters <- lowercaseLetters + 1
        if Char.IsUpper(txtNewPassword.Text, i) then
            uppercaseLetters <- uppercaseLetters + 1
    
    txtDigits.Text <- string digits
    txtSymbols.Text <- string symbols
    txtLowercaseLetters.Text <- string lowercaseLetters
    txtUppercaseLetters.Text <- string uppercaseLetters

    txtPasswordLength.Text <- string length

Here is an example of running the program:

The Type of Character in a String

The Type of Character in a String

The Type of Character in a String

Converting Characters to the Opposite Case

The English language uses two character representations: lowercase and uppercase. The characters in lowercase are: a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, and z. The equivalent characters in uppercase are represented as A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, and Z. Characters used for counting are called numeric characters; each one of them is called a digit. They are 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. There are other characters used to represent things in computer applications, mathematics, and others. Some of these characters, also called symbols are ~ , ! @ # $ % ^ & * ( ) _ + { } ` | = [ ] \ : " ; ' < > ? , . / These characters are used for various reasons and under different circumstances. For example, some of them are used as operators in mathematics or in computer programming. Regardless of whether a character is easily identifiable or not, all these symbols are character types and can be declared using the char data type followed by a name.

An alphabetic character can be converted from one case to another. The other characters, non-alphabetic symbols, and the numbers, do not have a case and therefore cannot be converted in cases.

To convert a string from lowercase to uppercase, you can call use the ToUpper() method of the String class. It is overloaded with two versions. One of the versions of this method uses the following signature:

member ToUpper : unit -> string

This method takes no argument. It considers each character of the string that called it. If the character is already in uppercase, it would not change. If the character is a lowercase alphabetic character, it would be converted to uppercase. If the character is not an alphabetic character, it would be kept "as-is".

To convert a string to lowercase, you can call the String.ToLower() method. Its signature is:

member ToLower : unit -> string

This method follows the same logic as its counterpart: it scans the string that called it, visiting each character. If the character is not an alphabetic character, it would be kept "as-is". If the character is an uppercase alphabetic character, it would be converted to lowercase. If it is in lowercase, it would not be converted.

Replacing a Character

If you have a string that contains a wrong character, you can either delete that character or replace it with another character of your choice. To support this operation, the String class is equipped with the Replace() method that is overloaded with two versions. One of the versions of the string.Replace() method uses the following syntax:

member Replace : 
        oldChar:char * 
        newChar:char -> string

The first argument of this method is used to identify the sought character. If and everywhere that character is found in the string, it would be replaced by the character passed as the second argument.

 
   
 

Characteristics of Strings

 

An Empty String

A string is referred to as empty if it contains no characters at all. Here is an example:

let empty : string = ""

A Null or Empty String

A string is referred to as null if it has lost its characters. For example, at one time a string may contain characters but those characters get deleted. If the string is/was stored in a variable, the variable may now contain garbage.

To let you find out whether a string is empty or null, the String class is equipped with a static method named IsNullOrEmpty. Its signature is:

static member IsNullOrEmpty : 
        value:string -> bool

A String With a White Space

A string contains a white space if it was previously initialized with at least one character and all of its characters have been removed or the string was simply initialized with something like the Space bar of the keyboard. Here is an example:

let whiteSpace : string = "    "

To let you find out whether a string is null or contains a white space, the String class is equipped with a static method named IsNullOrWhiteSpace. Its signature is:

static member IsNullOrWhiteSpace : 
        value:string -> bool

Operations on Strings

 

String Concatenation

One of the routine operations you can perform on two strings consists of adding one to another. There are two techniques you can use.

To add one string to another, you can use the addition operator as done in arithmetic. Besides the addition operator, to formally support string concatenation, the String class is equipped with a method named Concat. One of the versions of this method takes two String arguments. Its signature is:

static member Concat : 
        str0:string * 
        str1:string -> string

This version takes two strings that should be concatenated. The method returns a new string as the first added to the second. Two imitations of this version use the following signatures:

static member Concat : 
        str0:string * 
        str1:string * 
        str2:string -> string
static member Concat : 
        str0:string * 
        str1:string * 
        str2:string * 
        str3:string -> string

In each case, the method takes the number of strings and adds them.

Replacing a Sub-String

Inside of a string, if you have a combination of consecutive characters you don't want to keep, you can either remove that sub-string or replace it with a new combination of consecutive characters of your choice. To support this operation, the String class is equipped with a method named Replace. Its signature is:

member Replace : 
        oldValue:string * 
        newValue:string -> string

The oldStr argument is the sub-string to look for in the string. Whenever that sub-string is found in the string, it is replaced by the newStr argument.

Formatting a String

Formatting a string consists of specifying how it would be presented as an object. To support this operation, the String class is equipped with a static method named Format. The String.Format() method. One of its versions uses the following signature:

static member Format : 
        format:string * 
        arg0:Object -> string

This method takes two arguments. The first argument can contain one or a combination of % placeholders. The second argument contains one or a combination of values that would be added to the % placeholders of the first argument.

Copying a String

After declaring and initializing one string variable, you can assign it to another string variable using the assignment operator. Assigning one variable to another is referred to as copying it. To formally support this operator, the String class is equipped with the Copy() method. Its signature is:

static member Copy : 
        str:string -> string

This method takes as argument an existing String object and copies it, producing a new string.

Copying To a String

The String.Copy() method is used to copy all characters of one string into another string. To let you copy only a few characters, the String class is equipped with a method named CopyTo. Its signature is:

member CopyTo : 
        sourceIndex:int * 
        destination:char[] * 
        destinationIndex:int * 
        count:int -> unit

Strings Comparisons

 

Introduction

String comparison consists of examining the characters of two strings with a character of one string compared to a character of the other string with both characters at the same positions. To support this operation, the String class is equipped with the Compare() method. A version of this method uses the following signature:

static member Compare : 
        strA:string * 
        strB:string -> int

This method is declared static and it takes two arguments. When it starts, the first character of the first argument is compared to the first character of the second string. Alphabetically, if the first character of the first string has a lower alphabetical index than the first character of the second, this method returns a negative value. If the first character of the first string has a higher alphabetical index than the first character of the second, this method returns a positive value. If the first characters of both strings are the same, the method continues with the second character of each string. If both strings have the exact same characters, the method returns 0. This can be resumed as follows. The method returns:

  • A negative value if string1 is less than string2
  • 0 if string1 and string2 are equal
  • A positive value if string1 is greater than string2

When using this version of the String.Compare() method, the case (upper or lower) of each character is considered. If you don't want to consider this option, the String class proposes another version of the method. Its signature is:

static member Compare : 
        strA:string * 
        strB:string * 
        ignoreCase:bool -> int

The third argument allows you to ignore the case of the characters when performing the comparison.

String Equality

In the previous section, we saw that the indexed-equivalent characters of two strings can be compared to know whether one is lower or higher than the other's. If you are only interested to know whether two strings are equivalent, you can call the Equals() method of the String class. Two versions use the following signatures:

override Equals : 
        obj:Object -> bool
override Equals : 
        value:string -> bool

When calling one of these versions, use an Object object or a String variable that calls it. The method takes one argument. The variable that calls the method is compared to the value passed as argument. If both values are the exact same, the method returns true. The comparison is performed considering the case of each character. Here is an example:

open System
open System.Windows.Forms

let exercise = new Form(Width = 385, Height = 240, Text = "Create Passrod")

// New Password
exercise.Controls.Add(new Label(Left = 12, Top = 22, Width = 85, Text = "New Password:"))
let txtNewPassword = new TextBox(Left = 112, Top = 19, Width = 124)
exercise.Controls.Add txtNewPassword

// Password Length
let txtPasswordLength = new TextBox(Left = 242, Top = 19, Width = 20)
exercise.Controls.Add txtPasswordLength
exercise.Controls.Add(new Label(Left = 267, Top = 22, Width = 80, Text = "Characters"))

// Confirm Password
exercise.Controls.Add(new Label(Left = 12, Top = 51, Width = 100, Text = "Confirm Password:"))
let txtConfirmPassword = new TextBox(Left = 112, Top = 48, Width = 124)
exercise.Controls.Add txtConfirmPassword

// Lowercase Letters
let txtLowercaseLetters = new TextBox(Left = 112, Top = 78, Width = 20)
exercise.Controls.Add txtLowercaseLetters
exercise.Controls.Add(new Label(Left = 136, Top = 81, Width = 100, Text = "Lowercase Letters"))

// Uppercase Letters
let txtUppercaseLetters = new TextBox(Left = 242, Top = 78, Width = 20)
exercise.Controls.Add txtUppercaseLetters
exercise.Controls.Add(new Label(Left = 266, Top = 81, Width = 100, Text = "Uppercase Letters"))

// Digits
let txtDigits = new TextBox(Left = 112, Top = 108, Width = 20)
exercise.Controls.Add txtDigits
exercise.Controls.Add(new Label(Left = 136, Top = 111, Width = 100, Text = "Digits"))

// Symbols
let txtSymbols = new TextBox(Left = 242, Top = 111, Width = 20)
exercise.Controls.Add txtSymbols
exercise.Controls.Add(new Label(Left = 266, Top = 114, Width = 100, Text = "Symbols"))

let txtNewPasswordTextChanged e =
    let mutable digits = 0
    let mutable symbols = 0
    let mutable lowercaseLetters = 0
    let mutable uppercaseLetters = 0
    let length = txtNewPassword.Text.Length

    for i = 0 to length - 1 do
        if Char.IsDigit(txtNewPassword.Text.[i]) then
            digits <- digits + 1
        if Char.IsSymbol(txtNewPassword.Text.[i]) || Char.IsPunctuation(txtNewPassword.Text.[i]) then
            symbols <- symbols + 1
        if Char.IsLower(txtNewPassword.Text, i) then
            lowercaseLetters <- lowercaseLetters + 1
        if Char.IsUpper(txtNewPassword.Text, i) then
            uppercaseLetters <- uppercaseLetters + 1
    
    txtDigits.Text <- string digits
    txtSymbols.Text <- string symbols
    txtLowercaseLetters.Text <- string lowercaseLetters
    txtUppercaseLetters.Text <- string uppercaseLetters

    txtPasswordLength.Text <- string length
txtNewPassword.TextChanged.Add txtNewPasswordTextChanged

// Message
let lblMessage = new Label(Left = 109, Top = 182, Width = 200)
exercise.Controls.Add lblMessage

// Submit
let btnSubmit = new Button(Left = 112, Top = 146, Width = 75, Text = "Submit")
exercise.Controls.Add btnSubmit

let btnSubmitClick e =
    let strNewPassword = txtNewPassword.Text
    let strConfirmPassword = txtConfirmPassword.Text

    if strNewPassword.Equals(strConfirmPassword) = false then
        lblMessage.Text <- "The passwords are different"
    else
        lblMessage.Text <- ""

btnSubmit.Click.Add btnSubmitClick

// Close
let btnClose = new Button(Left = 193, Top = 146, Width = 75, Text = "Close")
exercise.Controls.Add btnClose

let btnCloseClick e = exercise.Close()
btnClose.Click.Add btnCloseClick

do Application.Run exercise

Here is an example of running the program:

Equality of Two Strings

Equality of Two Strings

Equality of Two Strings

If you don't want to consider the case, use the following version of the method:

member Equals : 
        value:string * 
        comparisonType:StringComparison -> bool

An alternative to the second signature is to use a static version of this method whose syntax is:

static member Equals : 
        a:string * 
        b:string -> bool

This method takes two String arguments and compares them. If they are the same, the method returns true. This method considers the cases of the characters. If you don't want this option taken into consideration, use the following version of the method:

member Equals : 
        value:string * 
        comparisonType:StringComparison -> bool

Working With Sub-Strings

 

Introduction

A sub-string is a section or part of a string. To create a sub-string, you first need a string and can retrieve one or more values from it. To support this, the String class is equipped with a method named Substring(). One version uses the following signature:

member Substring : 
        startIndex:int -> string

The integer argument specifies the position of the first character from the variable that called the method. The return value is a new string that is made of the characters from startIndex to the end of the string.

Probably the most consistent way to create a string is to control the beginning and end retrieved from the original string. To support this, the String class is equipped with another version of the Substring() method. Its signature is:

member Substring : 
        startIndex:int * 
        length:int -> string

The first argument specifies the index of the character to start from the string variable that calls this method. The second argument specifies the length of the string.

Looking for a Character or a Sub-String

To assist you with looking for a character or a sub-string within a string, the String class proposes a method named Contains. Its signature is:

member Contains : 
        value:string -> bool
 
 
   
 

Home Copyright © 2009-2015 FunctionX Home