Promo Offers XMAS Offers:
KVM VPS @ UP TO 50% OFF for 3 mos. Learn More Dedicated @ UP TO 56% OFF for 1 mo. Learn More
  • Your Cart is Empty
  • Cart
  • Log In

String

The string is a fundamental term for the computer programming sphere. It represents a kind of a data container storing all types of information and is used by most programming languages.

What is a string?

Most programming languages use various data types to define certain changeable values and to determine the operations performed on those values. Those data types, known as variables, may include integers, floating points, alphanumeric strings, etc. While the integers and floating numbers are used mainly for the execution of various mathematical operations (e.g. addition, subtraction, multiplication, etc.), the strings may contain any type of information, including letters, numbers, symbols, etc.

What is a string used for?

The main purpose of strings is to categorize/designate certain values in programming languages and even database systems. As a very important data type, they are practically used in every programming language or database system out there.

How is a string represented?

Each string represents an ordered set of alphanumeric characters that can include even punctuation marks and spaces. Strings may vary a lot according to the particular pre-defined character set (the alphabet used) and the character encoding method used (the way the bits represent them). For that reason, it has become a recently established practice for strings to be implemented with Unicode, which could ensure a universal character code for all written languages.

How is a string recognized?

For a piece of data to be recognized as a string and differentiated from a variable name or a number, it needs to be enclosed in single or double quotation marks. Some popular programming languages like PHP and Perl use both quotation mark types to set apart strings within a code, while others like the C language use double quotes only.

For instance, we could turn UNITED KINGDOM into a variable by just enclosing it within quotes (‘UNITED KINGDOM’ or “UNITED KINGDOM”).

The length of a string

The length of a string is usually defined by the number of characters it contains. In our example the string ‘UNITED KINGDOM’ is 14 characters long (13 characters plus the space between the two words). Some languages like C put an invisible character (in most cases a NULL character (\0) is used) at the end of a string to mark it, which may increase the actual string length with one symbol.

String Types

There isn’t a clearly defined typology of strings. However, there are certain strings, such as the empty strings, particularly useful for some programming applications. They contain no characters and thus have a zero length. There is another known string type - the substring, representing any contiguous sequence of characters within a string. As far as the string size is concerned, the strings can be divided intro strings with a fixed size and strings with unfixed size.

String Examples

Example 1:

Let’s see how a string could be used within an if-then statement in the PHP programming language.

if ("string1" == "string2") then ... 

In this example we set a condition that if the words string1 and string2 are identical the returned value will be true. If not, the result will be false. Here the two words being compared are treated as strings. This example is just to illustrate how the data contained in the string is used during certain operations and has no practical value.

Example 2:

Here is an example that could be put in practice. Suppose you have an internal company website where your employees log in every day to start/stop their work time and check important information about upcoming company events. To secure the site from any unauthorized access from outside the company, you will need to set a password protected login form.

There will be one general username/password set for your employees to input in the form to access the portal. Now you will need to set up a method for matching the values the users put in the form against the pre-defined values to verify whether the correct details are entered and the employee is permitted access to the site. To do that, we can use the same ‘if-then’ conditional operator to compare the values introduced in the login form by the employee with the pre-defined username and password values:

if ($_POST['username'] != "administrator" || $_POST['password'] != "secret") then
exit("Invalid username or password."); 

In this example, the username and the password typed by the user in the login form are represented by the ($_POST['username'] and $_POST['password'] strings, respectively, while the static values of the pre-defined login details are marked with "administrator" and "secret".

If the two values match – the user will be permitted to enter the site, if not – an error message like ‘Invalid username or password’ will be returned on the screen.