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
1935
votes
30 answers

How do I split the definition of a long string over multiple lines?

I have a very long query. I would like to split it in several lines in Python. A way to do it in JavaScript would be using several sentences and joining them with a + operator (I know, maybe it's not the most efficient way to do it, but I'm not…
Pablo Mescher
  • 26,057
  • 6
  • 30
  • 33
1913
votes
38 answers

How do I split a string in Java?

I want to split the string "004-034556" into two strings by the delimiter "-": part1 = "004"; part2 = "034556"; That means the first string will contain the characters before '-', and the second string will contain the characters after '-'. I also…
riyana
  • 21,385
  • 10
  • 35
  • 34
1891
votes
15 answers

Why does this code using random strings print "hello world"?

The following print statement would print "hello world". Could anyone explain this? System.out.println(randomString(-229985452) + " " + randomString(-147909649)); And randomString() looks like this: public static String randomString(int i) { …
0x56794E
  • 20,883
  • 13
  • 42
  • 58
1800
votes
18 answers

How to check if a string "StartsWith" another string?

How would I write the equivalent of C#'s String.StartsWith in JavaScript? var haystack = 'hello world'; var needle = 'he'; haystack.startsWith(needle) == true Note: This is an old question, and as pointed out in the comments ECMAScript 2015 (ES6)…
sol
1753
votes
35 answers

How do I create a Java string from the contents of a file?

I've been using the idiom below for some time now. And it seems to be the most wide-spread, at least on the sites I've visited. Is there a better/different way to read a file into a string in Java? private String readFile(String file) throws…
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
1741
votes
29 answers

How to convert a string to lower case in Bash

Is there a way in bash to convert a string into a lower case string? For example, if I have: a="Hi all" I want to convert it to: "hi all"
assassin
  • 19,899
  • 10
  • 30
  • 43
1717
votes
36 answers

Random string generation with upper case letters and digits

How do I generate a string of size N, made of numbers and uppercase English letters such as: 6U1S75 4Z4UKK U911K4
Hellnar
  • 62,315
  • 79
  • 204
  • 279
1689
votes
34 answers

startsWith() and endsWith() functions in PHP

How can I write two functions that would take a string and return if it starts with the specified character/string or ends with it? For example: $str = '|apples}'; echo startsWith($str, '|'); //Returns true echo endsWith($str, '}'); //Returns true
Ali
  • 261,656
  • 265
  • 575
  • 769
1634
votes
20 answers

How do I reverse a string in Python?

There is no built in reverse function for Python's str object. What is the best way of implementing this method? If supplying a very concise answer, please elaborate on its efficiency. For example, whether the str object is converted to a different…
oneself
  • 38,641
  • 34
  • 96
  • 120
1622
votes
31 answers

How do I get the filename without the extension from a path in Python?

How do I get the filename without the extension from a path in Python? "/path/to/some/file.txt" → "file"
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
1574
votes
14 answers

Convert integer to string in Python

How do I convert an integer to a string? 42 ⟶ "42" For the reverse, see How do I parse a string to a float or int?. Floats can be handled similarly, but handling the decimal points can be tricky because floating-point values are not precise.…
Hick
  • 35,524
  • 46
  • 151
  • 243
1434
votes
5 answers

Best way to convert string to bytes in Python 3?

TypeError: 'str' does not support the buffer interface suggests two possible methods to convert a string to bytes: b = bytes(mystring, 'utf-8') b = mystring.encode('utf-8') Which method is more Pythonic? See Convert bytes to a string for the…
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
1428
votes
23 answers

How to do case insensitive string comparison?

How do I perform case insensitive string comparison in JavaScript?
flybywire
  • 261,858
  • 191
  • 397
  • 503
1411
votes
16 answers

Convert JavaScript String to be all lowercase

How can I convert a JavaScript string value to be in all lowercase letters? Example: "Your Name" to "your name"
Derek
  • 16,181
  • 3
  • 27
  • 36
1399
votes
14 answers

Multiline string literal in C#

Is there an easy way to create a multiline string literal in C#? Here's what I have now: string query = "SELECT foo, bar" + " FROM table" + " WHERE id = 42"; I know PHP has <<
Chet
  • 21,375
  • 10
  • 40
  • 58