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
1206
votes
15 answers

How do I trim whitespace?

Is there a Python function that will trim whitespace (spaces and tabs) from a string? So that given input " \t example string\t " becomes "example string".
Chris
  • 21,549
  • 25
  • 71
  • 99
1198
votes
15 answers

Remove all whitespace in a string

I want to eliminate all the whitespace from a string, on both ends, and in between words. I have this Python code: def my_handle(self): sentence = ' hello apple ' sentence.strip() But that only eliminates the whitespace on both sides of…
not 0x12
  • 19,360
  • 22
  • 67
  • 133
1192
votes
42 answers

Converting an object to a string

How can I convert a JavaScript object into a string? Example: var o = {a:1, b:2} console.log(o) console.log('Item: ' + o) Output: Object { a=1, b=2} // very nice readable output :) Item: [object Object] // no idea what's inside :(
user680174
  • 11,921
  • 3
  • 15
  • 3
1168
votes
12 answers

How to escape braces (curly brackets) in a format string in .NET

How can brackets be escaped in using string.Format? For example: String val = "1,2,3" String.Format(" foo {{0}}", val); This example doesn't throw an exception, but it outputs the string foo {0}. Is there a way to escape the brackets?
Pop Catalin
  • 61,751
  • 23
  • 87
  • 115
1160
votes
3 answers

How to convert list to string

How can I convert a list to a string using Python?
Nm3
  • 11,707
  • 3
  • 16
  • 4
1147
votes
12 answers

How do I check if string contains substring?

I have a shopping cart that displays product options in a dropdown menu and if they select "yes", I want to make some other fields on the page visible. The problem is that the shopping cart also includes the price modifier in the text, which can be…
Jordan Garis
  • 11,489
  • 3
  • 16
  • 4
1144
votes
30 answers

endsWith in JavaScript

How can I check if a string ends with a particular character in JavaScript? Example: I have a string var str = "mystring#"; I want to know if that string is ending with #. How can I check it? Is there a endsWith() method in JavaScript? One…
Phani Kumar Bhamidipati
  • 13,183
  • 6
  • 24
  • 27
1135
votes
16 answers

How to convert UTF-8 byte[] to string

I have a byte[] array that is loaded from a file that I happen to known contains UTF-8. In some debugging code, I need to convert it to a string. Is there a one-liner that will do this? Under the covers it should be just an allocation and a memcopy,…
BCS
  • 75,627
  • 68
  • 187
  • 294
1131
votes
12 answers

Convert a String representation of a Dictionary to a dictionary

How can I convert the str representation of a dict, such as the following string, into a dict? s = "{'muffin' : 'lolz', 'foo' : 'kitty'}" I prefer not to use eval. What else can I use? The main reason for this, is one of my coworkers classes he…
UberJumper
  • 20,245
  • 19
  • 69
  • 87
1127
votes
11 answers

How to concatenate (join) items in a list to a single string

How do I concatenate a list of strings into a single string? For example, given ['this', 'is', 'a', 'sentence'], how do I get "this-is-a-sentence"? For handling a few strings in separate variables, see How do I append one string to another in…
alvas
  • 115,346
  • 109
  • 446
  • 738
1114
votes
13 answers

In Bash, how can I check if a string begins with some value?

I would like to check if a string begins with "node" e.g. "node001". Something like if [ $HOST == node* ] then echo yes fi How can I do it correctly? I further need to combine expressions to check if HOST is either "user1" or begins with…
Tim
  • 1
  • 141
  • 372
  • 590
1112
votes
10 answers

What is the correct way to check for string equality in JavaScript?

What is the correct way to check for equality between Strings in JavaScript?
JSS
  • 11,137
  • 3
  • 16
  • 3
1107
votes
15 answers

Are multi-line strings allowed in JSON?

Is it possible to have multi-line strings in JSON? It's mostly for visual comfort so I suppose I can just turn word wrap on in my editor, but I'm just kinda curious. I'm writing some data files in JSON format and would like to have some really long…
Anonnobody
  • 11,239
  • 2
  • 18
  • 6
1104
votes
20 answers

StringBuilder vs String concatenation in toString() in Java

Given the 2 toString() implementations below, which one is preferred: public String toString(){ return "{a:"+ a + ", b:" + b + ", c: " + c +"}"; } or public String toString(){ StringBuilder sb = new StringBuilder(100); return…
non sequitor
  • 18,296
  • 9
  • 45
  • 64
1086
votes
26 answers

Extract substring in Bash

Given a filename in the form someletters_12345_moreleters.ext, I want to extract the 5 digits and put them into a variable. So to emphasize the point, I have a filename with x number of characters then a five digit sequence surrounded by a single…
Berek Bryan
  • 13,755
  • 10
  • 32
  • 43