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
1372
votes
23 answers

Convert JS object to JSON string

If I defined an object in JS with: var j={"name":"binchen"}; How can I convert the object to JSON? The output string should be: '{"name":"binchen"}'
Bin Chen
  • 61,507
  • 53
  • 142
  • 183
1363
votes
20 answers

Trim string in JavaScript

How do I remove all whitespace from the start and end of the string?
Vinod
  • 31,933
  • 35
  • 96
  • 119
1356
votes
12 answers

What does the 'b' character do in front of a string literal?

Apparently, the following is the valid syntax: b'The string' I would like to know: What does this b character in front of the string mean? What are the effects of using it? What are appropriate situations to use it? I found a related question…
Jesse Webb
  • 43,135
  • 27
  • 106
  • 143
1355
votes
13 answers

How do I trim whitespace from a string?

How do I remove leading and trailing whitespace from a string in Python? " Hello world " --> "Hello world" " Hello world" --> "Hello world" "Hello world " --> "Hello world" "Hello world" --> "Hello world"
robert
1336
votes
27 answers

How can I read a text file into a string variable and strip newlines?

I have a text file that looks like: ABC DEF How can I read the file into a single-line string without newlines, in this case creating a string 'ABCDEF'? For reading the file into a list of lines, but removing the trailing newline character from…
klijo
  • 15,761
  • 8
  • 34
  • 49
1309
votes
52 answers

How to trim whitespace from a Bash variable?

I have a shell script with this code: var=`hg st -R "$path"` if [ -n "$var" ]; then echo $var fi But the conditional code always executes, because hg st always prints at least one newline character. Is there a simple way to strip whitespace…
too much php
  • 88,666
  • 34
  • 128
  • 138
1305
votes
15 answers

Why does comparing strings using either '==' or 'is' sometimes produce a different result?

Two string variables are set to the same value. s1 == s2 always returns True, but s1 is s2 sometimes returns False. If I open my Python interpreter and do the same is comparison, it succeeds: >>> s1 = 'text' >>> s2 = 'text' >>> s1 is s2 True Why is…
jottos
  • 20,098
  • 9
  • 30
  • 27
1277
votes
21 answers

How do I check if a string contains another string in Objective-C?

How can I check if a string (NSString) contains another smaller string? I was hoping for something like: NSString *string = @"hello bla bla"; NSLog(@"%d",[string containsSubstring:@"hello"]); But the closest I could find was: if ([string…
Jonathan.
  • 53,997
  • 54
  • 186
  • 290
1271
votes
26 answers

Count the number of occurrences of a character in a string

How do I count the number of occurrences of a character in a string? e.g. 'a' appears in 'Mary had a little lamb' 4 times.
Mat
  • 82,161
  • 34
  • 89
  • 109
1265
votes
17 answers

Converting 'ArrayList to 'String[]' in Java

How might I convert an ArrayList object to a String[] array in Java?
Alex
  • 16,375
  • 7
  • 22
  • 19
1251
votes
29 answers

Convert a string to an enum in C#

What's the best way to convert a string to an enumeration value in C#? I have an HTML select tag containing the values of an enumeration. When the page is posted, I want to pick up the value (which will be in the form of a string) and convert it to…
Ben Mills
  • 27,454
  • 14
  • 42
  • 38
1227
votes
39 answers

Converting from a string to boolean in Python

How do I convert a string into a boolean in Python? This attempt returns True: >>> bool("False") True
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
1222
votes
7 answers

Ukkonen's suffix tree algorithm in plain English

I feel a bit thick at this point. I've spent days trying to fully wrap my head around suffix tree construction, but because I don't have a mathematical background, many of the explanations elude me as they start to make excessive use of mathematical…
Nathan Ridley
  • 33,766
  • 35
  • 123
  • 197
1216
votes
12 answers

How to compare strings in Bash

How do I compare a variable to a string (and do something if they match)?
Erik Sapir
  • 23,209
  • 28
  • 81
  • 141
1213
votes
11 answers

How to convert a string to lower or upper case in Ruby

How do I take a string and convert it to lower or upper case in Ruby?
Heat Miser
  • 19,438
  • 7
  • 35
  • 38