Questions tagged [string]

A string is a finite sequence of symbols, commonly used for text, though sometimes for arbitrary data.

A string is a finite sequence of symbols, commonly used for text, though sometimes for arbitrary data.

Most programming languages provide a dedicated string data type or more general facilities and conventions for handling strings; as well as providing a way to denote string literals. In some programming languages everything is a string, for example in Tcl. A dedicated support library of differing sophistication is mostly provided as well.

String representations vary widely in the features they offer; the right string type can easily decrease the order of algorithms, while the wrong one might not even be able to accommodate your string data at all.

The following are some hand-picked representatives:

  • Zero-terminated Strings (aka. C-strings, ASCIZ, sz) are arrays of non-null elements, terminated by a special, null element (variants using a different terminating symbol are mostly restricted to old systems, e.g. DOS supported $).
  • Counted String (aka Pascal Strings) are arrays of arbitrary bytes, prefixed by a length indicator. Nowadays, the size for counted strings is restricted by available address space, though it was quite common to use a single byte for length (implying maximum length of 255).
  • Ropes, which are lists of segments (for example length + pointers into modifiable and non-modifiable buffers), for efficient insertion and deletion.

Many (especially functional) languages support strings as a list of base symbols.

For Unicode support, a special string of the strings type is getting common, as Unicode characters can be of arbitrary length, even in UTF-32. This enables efficient character-indexing by pushing the complexities of the character set into the string type.

In most languages, strings can be iterated over, similar to lists/arrays. In some high-level languages (in which strings are a data type unto themselves), strings are immutable, so string operations create new strings.

For text strings, many encodings are in used, though modern usage is converging on Unicode, using UTF-8 (some early adopters of Unicode instead transitioned form UCS2 to UTF-16 as a persistence format).

Windows software often adopts the WinAPI convention of using UTF-16 internally, converting for external data and persistence instead of system calls.

A String Literal is an occurrence of a string phrase in source code, generally encapsulated in dedicated delimiters (for example, in C/C++ and Java a String literal is surrounded by double quotes - "This is a String Literal").

Useful Links:

183393 questions
2699
votes
32 answers

Check if a variable is a string in JavaScript

How can I determine whether a variable is a string or something else in JavaScript?
Olical
  • 39,703
  • 12
  • 54
  • 77
2657
votes
36 answers

How do I check if a string contains a specific word?

Consider: $a = 'How are you?'; if ($a contains 'are') echo 'true'; Suppose I have the code above, what is the correct way to write the statement if ($a contains 'are')?
Charles Yeung
  • 38,347
  • 30
  • 90
  • 130
2630
votes
16 answers

How do I get a substring of a string in Python?

I want to get a new string from the third character to the end of the string, e.g. myString[2:end]. If omitting the second part means 'to the end', and if you omit the first part, does it start from the start?
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
2553
votes
9 answers

How do I break a string in YAML over multiple lines?

I have a very long string: Key: 'this is my very very very very very very long string' I would like to express it over multiple shorter lines, e.g., Key: 'this is my very very very ' + 'long string' I would like to use quotes as above, so…
jjkparker
  • 27,597
  • 6
  • 28
  • 29
2513
votes
9 answers

How do I lowercase a string in Python?

Is there a way to convert a string to lowercase? "Kilometers" → "kilometers" See How to change a string into uppercase? for the opposite.
Benjamin Didur
  • 25,343
  • 3
  • 15
  • 5
2427
votes
23 answers

How do I escape curly-brace ({}) characters in a string while using .format (or an f-string)?

Non-working example: print(" \{ Hello \} {0} ".format(42)) Desired output: {Hello} 42
Schitti
  • 25,489
  • 8
  • 24
  • 21
2356
votes
41 answers

How do I get a consistent byte representation of strings in C# without manually specifying an encoding?

How do I convert a string to a byte[] in .NET (C#) without manually specifying a specific encoding? I'm going to encrypt the string. I can encrypt it without converting, but I'd still like to know why encoding comes to play here. Also, why should…
Agnel Kurian
  • 57,975
  • 43
  • 146
  • 217
2324
votes
32 answers

How to convert a string to an integer in JavaScript

How do I convert a string to an integer in JavaScript?
None
2283
votes
23 answers

Are double and single quotes interchangeable in JavaScript?

Consider the following two alternatives: console.log("double"); console.log('single'); The former uses double quotes around the string, whereas the latter uses single quotes around the string. I see more and more JavaScript libraries out there…
aemkei
  • 11,076
  • 8
  • 37
  • 29
2104
votes
19 answers

How do I pad a string with zeroes?

How do I pad a numeric string with zeroes to the left, so that the string has a specific length?
Faisal
2096
votes
31 answers

How to convert int to string in C++?

How can I convert from int to the equivalent string in C++? I am aware of two methods. Is there another way? (1) int a = 10; char *intStr = itoa(a); string str = string(intStr); (2) int a = 10; stringstream ss; ss << a; string str = ss.str();
Nemo
  • 24,540
  • 12
  • 45
  • 61
2080
votes
26 answers

How to check if the string is empty in Python?

Does Python have something like an empty string variable where you can do: if myString == string.empty: Regardless, what's the most elegant way to check for empty string values? I find hard coding "" every time for checking an empty string not as…
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
2065
votes
10 answers

Why is it string.join(list) instead of list.join(string)?

This has always confused me. It seems like this would be nicer: ["Hello", "world"].join("-") Than this: "-".join(["Hello", "world"]) Is there a specific reason it is like this?
Evan Fosmark
  • 98,895
  • 36
  • 105
  • 117
2025
votes
28 answers

How to read a file line-by-line into a list?

How do I read every line of a file in Python and store each line as an element in a list? I want to read the file line by line and append each line to the end of the list.
Julie Raswick
  • 20,403
  • 3
  • 16
  • 3
1958
votes
46 answers

How to generate a random alpha-numeric string

I've been looking for a simple Java algorithm to generate a pseudo-random alpha-numeric string. In my situation it would be used as a unique session/key identifier that would "likely" be unique over 500K+ generation (my needs don't really require…
Todd
  • 3,363
  • 3
  • 21
  • 12