Posts Tagged ‘substring’

There are a few ways to find out if a string contains a substring using bash. Below are a couple of ways this can be done without invoking any other processes. Star Wildcard One very simple method is to match strings using the * character to denote any number of other characters. For example: if […]

Reversing a string in Python

Posted: 25th April 2013 by Tim in Python
Tags: , , , , , , , ,

If you need to reverse a string in python, the easiest way is to do use the my_string[::-1] syntax. For example, to print a reversed string, you could write: my_string = “ABCDE” print my_string[::-1] So what is this doing? The square bracket syntax is used for returning a substring. This works like so: [<start>:<end>:<step>]. If […]

If you want to get the last n characters of a string in bash, you can simply mix bash substrings – ${string:offset[:length]} – with bash string lengths – ${#string}. For example: str=”abcde” n=3 echo ${str:${#str} – $n} will print: cde

Substrings in Bash

Posted: 17th October 2010 by Tim in Bash
Tags: , , , , , , , ,

There are a number of ways to extract parts of a string in bash. If you know the position of the substring you’re looking for, then you can use the ${string:offset[:length]} syntax. This works by providing a string, an offset (or starting position – remember that the first letter is in position 0) and, optionally, […]