logo

Polyglot CheatSheet - String

Last Updated: 2023-03-03

Quotes

A raw string literal is a string in which the escape characters like \n, \t or \" are not processed.

C++

  • Single quote ('): single character
  • Double quote ("): string

Raw string literal:

R "delimiter( raw_characters )delimiter"

delimiter is optional. One common usecase is to create ProtoBufs in tests:

R"pb({
 ...
})pb";

Java

  • Single quote ('): single character
  • Double quote ("): string
  • Text blocks:
String foo = """
    bar
    baz
""";

Python

Single quotes (') and double quotes (") are quivelent.

Backtick quoting is generally non-useful and gone in Python 3.

Multiline strings: triple quotations using either single quotes ''' or double quotes """.

Python docstrings (triple quotations using either single quotes ''' or double quotes """) are the string literals that appear right after the definition of a function, method, class, or module.

def foo():
    '''This is the docstring.'''
    pass

JavaScript

Single quotes (') and double quotes (") are quivelent.

Template literals (Template strings) (`):

  • allow for multi-line strings.
  • string interpolation, e.g. hello ${name}!.

Go

  • Double quote ("): need to escape special characters, e.g. "\n" is a single character
  • backtick (`): raw string literals, no escape.

String Comparison

C++

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

str1 == str2

Substring

JavaScript

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

Starts With

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")

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'