Chapter 4. Handling String Expressions

In this chapter, we will cover:

  • Appending to a string
  • Formatting a string
  • Matching a regular expression within a string
  • Performing character substitution on a string
  • Parsing a string using conversion specifiers
  • Determining the length of a string
  • Comparing strings
  • Comparing a string of characters
  • Locating the first instance of a character
  • Locating the index of a character
  • Determining the class of a string
  • Locating the last instance of a string
  • Determining the size of a string
  • Replacing values within a string
  • Locating a pattern within a string
  • Returning a range of characters from a string
  • Creating a string of repeated characters
  • Replacing ranges of characters contained within a string
  • Creating a reverse string
  • Converting a string to lowercase
  • Converting a string to title
  • Converting a string to uppercase
  • Trimming a string
  • Trimming leading whitespace
  • Trimming trailing whitespace
  • Locating the word end
  • Locating the word start
  • Performing variable substitution

Introduction

When I first started using Tcl, everything I read or researched stressed the mantra "Everything is a string". Coming from a hard-typed coding environment, I was used to declaring variable types and in Tcl this was not needed. A set command could—and still does—create the variable and assigns the type on the fly. For example, set variable "7" and set variable 7 will both create a variable containing 7. However, with Tcl, you can still print the variable containing a numeric 7 and add 1 to the variable containing a string representation of 7.

It still holds true today that everything in Tcl is a string. When we explore the Tk Toolkit and widget creation, you will rapidly see that widgets themselves have a set of string values that determine their appearance and/or behavior.

As a pre-requisite for the recipes in this chapter, launch the Tcl shell as appropriate for your operating system. You can access Tcl from the command line to execute the commands.

As with everything else we have seen, Tcl provides a full suite of commands to assist in handling string expressions. However due to the sheer number of commands and subsets, I won't be listing every item individually in the following section. Instead we will be creating numerous recipes and examples to explore in the following sections. Please refer to the Tcl/Tk Commands listing at the end of this book. A general list of the commands is as follows:

Command

Description

string

The string command contains multiple keywords (see the section covering the command) allowing for manipulation and data gathering functions.

append

Appends to a string variable.

format

Format a string in the same manner as C sprint.

regexp

Regular expression matching.

regsub

Performs substitution, based on Regular expression matching.

scan

Parses a string using conversion specifiers in the same manner as C sscanf.

subst

Perform backslash, command, and variable substitution on a string.

Using the commands listed in the table, a developer can address all their needs as applies to strings. In the following sections, we will explore these commands as well as many subsets of the string command.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.103.204