Posts Tagged ‘string’

If you have a filename or list of filenames, you may want to strip the extension. There are a few ways of “detecting” which part of the filename is the extension, but may not work if your file has multiple extensions (e.g. .tar.gz), contains spaces or periods, or meets other weird criteria. If you know […]

Counting the number of occurrences of a given character in a std::string can be done using one function from the STL library: the std::count(…) function. This function takes three parameters: two iterators (the beginning and end of the desired search), and the item you wish to count. This function can be used for any STL […]

Strings in python can be split using any given delimeter. Unlike other languages, the delimeter can be a string of any length; it’s not limited to one character. To do this, you can use the <string>.split(<delimeter>) function. Similarly, you can join an array of strings back into one string, using any defined string to join […]

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 […]

Imagine that you have read in a hexadecimal string from the command line, a config file or whatever, and you want to use this value in your program. To do this, you need to do a string to integer conversion, but in base 16 since it’s a hexadecimal string. To do this, you can use […]

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

If you’ve got a string of items in bash which are delimited by a common character (comma, space, tab, etc) you can split that into an array quite easily. Simply (re)define the IFS variable to the delimiter character and assign the values to a new variable using the array=($<string_var>) syntax. The new variable will now […]

Reading from a file in C

Posted: 31st October 2010 by Tim in C
Tags: , , , , , , , ,

Reading data from a file is fairly common. The stdio.h provides us with a function, getline, which allows us to read lines from a file without worrying about buffer overflows and other memory corruption issues that C is famous for. The following code opens a file named “myfile.txt” and prints out each line with the […]

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, […]

Reading from a file in C++

Posted: 27th September 2010 by Tim in C++
Tags: , , , , , , , , ,

Reading data from a file is common in programming. C++ makes this process fairly painless with the ifstream class. When combined with the string class, you can avoid the memory and buffer overflow issues which you would have to deal with in C. The following example opens a file named “myfile.txt” and prints out each […]

It’s often useful to concatenate two fields together in an SQL query. In PostgreSQL, fields may be concatenated using the || operator. The syntax is really simple. Just place the double bar between the fields you want to join, and (optionally) give a label to that new field. Strings can also be concatenated with the […]

In Bash you quite often need to check to see if a variable has been set or has a value other than an empty string. This can be done using the -n or -z string comparison operators. The -n operator checks whether the string is not null. Effectively, this will return true for every case […]