Skip to content

Instantly share code, notes, and snippets.

@sarrionandia
Last active November 9, 2015 11:31
Show Gist options
  • Save sarrionandia/ed01aec35a25f1aba8ce to your computer and use it in GitHub Desktop.
Save sarrionandia/ed01aec35a25f1aba8ce to your computer and use it in GitHub Desktop.
String Manipulation

#String Manipulation This is a free revision guide for GCSE Computer Science created by Tito Sarrionandia for http://LearnCompSci.uk

##Introduction As you will recall from the Selecting & Using Datatypes section, a String is a datatype. It is made up of a series of individual characters. They are used to represent textual data. For example, if you send a message to a friend online, the message is probably stored as a string in the messaging program.

We often use double quotes to represent a string, we will stick to that convention for this guide, like this:-

"Hello, World!"

Everything inside the quotes is part of the string.

Remember:

"%$*&^.,\£"1532: Strings can contain punctuation, numbers and special characters

"": Strings can also be empty!

What is String Manipulation?

We often want to change strings in some way to make them suitable for what we are doing. We call this String Manipulation.

For example: If a program wanted the user to enter M or F (uppercase) when selecting a gender, we know there is some chance that they will actually type in a lowercase letter.

We could use string manipulation to capitalise their input before we compare it to M or F.

There are many types of manipulation available. These are provided by most programming languages as String functions or operations (things you can do to a string).

The manipulations we will examine in this guide are:--

  • [Capitalisation] (#capitalisation)
  • [Concatenation] (#concatenation)
  • [Character Extraction] (#character-extraction)
  • [Substring] (#substring)
  • [Reverse] (#reverse)
  • [Regular Expressions] (#regular-expressions)
  • [Substitution] (#substitution)

###Capitalisation One simple manipulation is capitalisation. It converts all of the letters in the string to their uppercase equivalent. In Python, the capitalisation function is called upper

We can also perform the opposite action, in Python this function is called lower.

Upper:

`"Hello".upper()
:: "HELLO"

Lower:

"Hello World!".lower()
:: "hello world!"

###Concatenation Concatenation means joining two strings together. It is often denoted with a + or a .

"I am your father, " + "Luke"

:: "I am your father, Luke"

This is useful when you want the user to type in a value that is later output by the program.

###Character Extraction This is used to get the value of a particular character in the string. Since a string is just an array of characters, many languages let you use the square bracket notation to select characters from strings. Just like with arrays, we start counting at 0.

For example, if we wanted to get the first letter from the string "Hello", we could run:

"Hello"[0]

:: 'H'

To get the 5th letter:

"Hello"[4]

:: 'o'

However, if we try to access the 6th letter:

"Hello"[5]

::Error (The string is only 5 characters long)

###Substring Substring selects text from the middle of a string. Most languages require that you specify the index of where you want the string to start, and the length of the substring you want.

For example:

myString = "Cat and dog" myString.substring(4, 3)

Will give us:

:: "and"

This is because the word "and" starts at character 4 and continues for the next 3 characters.

  C      |        a     |   t   |   |   a   |   n   |    d   |   |   d   |   o   |   g   

------------- | ------------- | ------ | ----- | ----- | ----- | ----- | ----- | ----- | ----- | ----- 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10

###Reverse Reversing is a simple string manipulation, it just returns a backwards copy of the string.

For example:

originalString = "Pineapple" pineapple.reverse() :: "elppaeniP"`

###Regular Expressions Regular expressions are a powerful way of finding patterns in a string. For example, [0-9] means any number, and {3,} means three or more. So if you ran the regular expression [0-9]{3,} on the following text, the bold parts would be returned as results:

Today is the 12th February 2003. The phone number for this office is 01234543234

This is useful if you want to make a complex check on a piece of text. For example, you could check whether or not an email address is valid by checking that it has a number of characters, followed by an @ symbol, followed by some more characters, then a ., then a top-level domain (such as .co.uk).

###Substitution String substitution lets you create complex output for text-based programs. Many languages, such as C and Python, use the % notation for substitution.

If you wanted a program to output somebody’s name and age, you could write the output as follows

Name: %S Age: %d”

This would let you swap the %S for a string (the users name), and the %d for an integer (the user’s age); this is neater than using many concatenations to do the same job.

##License Creative Commons 4.0 ShareAlike
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

You can do whatever you want with this guide, just as long as:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment