logo

String

Last Updated: 2024-01-20

Palindrome

while (lo < hi) {
    if s[lo] != s[hi]
    lo++;
    hi--;
}

Reverse a string

void reverse(char* str)
{
    char *low = str,  *high = str;
    while (*high != '\0') {
        high++;
    }
    high--;
    while (low < high){
        char tmp = *low;
        *low++ = *high;
        *high-- = tmp;
    }
}

Levenshtein Distance

public static int LevenshteinDistance(char[] a, char[] b){

    int [][] cnt = new int[a.length + 1][b.length + 1];
    for (int i = 0; i <= a.length; i++) {
        cnt[i][0] = i;
    }
    for (int i = 0; i <= b.length; i++) {
        cnt[0][i] = i;
    }
    for (int i = 1; i <= a.length; i++) {
        for (int j = 1; j<= b.length; j++){
            if (a[i-1] == b[j-1])
                cnt[i][j] = cnt[i-1][j-1];
            else {
                int smallest = cnt[i-1][j-1] + 1;
                if (cnt[i-1][j] + 1 < smallest)
                    smallest = cnt[i-1][j] + 1;
                if (cnt[i][j-1] + 1 < smallest)
                    smallest = cnt[i][j-1] + 1;
                cnt[i][j] = smallest;
            }
        }
    }
    return cnt[a.length][b.length];
}

String Comparison

C++

string::operator==() is equivalent to string::compare()

str1 == str2

Substring

JavaScript

str.substring(startIndex, endIndex);
  • startIndex: required, inclusive
  • endIndex: optional, exclusive

StartsWith / EndsWith

C++

#include "absl/strings/match.h"
absl::StartsWith(str, prefix)

Python

>>> 'asdf'.startswith('as')
True

JavaScript

> "asdf".startsWith("as")
true

Go

strings.HasPrefix("string", "prefix")
strings.HasSuffix(s, "!")

Trim / Strip

C++

Strip prefix

absl::StripPrefix

Java

// since Java 11
stripLeading()
stripTrailing()

// Removes the white space from both ends
// Unicode-aware
// since Java 11
strip()

// Removes the white space from both ends
// only characters <= U+0020 (space)
trim()

Python

>>> ' asdf '.strip()
'asdf'

JavaScript

> " asdf ".trim()
"asdf"

Remove Trailing Slash

JavaScript

str.endsWith('/') ? str.slice(0, -1) : str;
str.endsWith('/') ? str.substr(0, str.length - 1) : str;

Remove Prefix

JavaScript

'www.example.com'.replace('www.', '');
'www.example.com'.replace(/^www\./, '');
'www.example.com'.slice(4);

Split

C++

use absl::StrSplit() from absl/strings/str_split.h

// Using std::string elements will make a copy of the pieces.
std::vector<std::string> letters = absl::StrSplit("a b c d e f",
                                                   absl::ByChar(' '));
// The string_views refer to memory from the input string.
std::list<absl::string_view> words = absl::StrSplit("the:quick;brown,fox",
                                                    absl::ByAnyChar(":;,"));

Go

import "strings"

result := strings.Split(str1, ",")

JavaScript

E.g. split by whitespaces.

str.split(/\s+/);

PHP

$parts = Str::explode('_', $raw);

Hack

$parts = Str\split($raw, '_');

Or

list($part1, $part2, $part3) = Str\split(",", $raw);

Python

>>> "a b c".split()
['a', 'b', 'c']

Repeated Characters

Python

>>> 'a' * 10
'aaaaaaaaaa'

Java

jshell> String s = IntStream.range(0, 10).mapToObj(i -> "a").collect(Collectors.joining(""))
s ==> "aaaaaaaaaa"

Join

C++

#include "third_party/absl/strings/str_join.h"
absl::StrJoin(std::make_tuple(a, b, c), "|");

Java

String.join(",", list);

Go

strings.Join(arr, ",")

Concatenate Strings

Kotlin

Suppose a and b are strings:

  • Use String interpolation: val s = "$a $b"
  • Use + or plus(): val s = a + b or val s = a.plus(b)
  • Use StringBuilder: val s = StringBuilder(); s.append(a).append(b); s.toString()

Translation

Python

maketrans(): creates a one to one mapping of a character to its translation / replacement.

str.maketrans()
bytes.maketrans()
bytearray.maketrans(from, to)

String Format

Python

>>> "hello {}".format("world")
'hello world'
>>> "{} {}".format("hello", "world")
'hello world'