Skip to content

Instantly share code, notes, and snippets.

@kinshuk4
Forked from sarrionandia/StringManipulation.md
Created October 27, 2015 11:34
Show Gist options
  • Save kinshuk4/f54cfe8fde84a89337cd to your computer and use it in GitHub Desktop.
Save kinshuk4/f54cfe8fde84a89337cd 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
  • Regular Expressions
  • 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

##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