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
982
votes
31 answers

How to convert an instance of std::string to lower case

I want to convert a std::string to lowercase. I am aware of the function tolower(). However, in the past I have had issues with this function and it is hardly ideal anyway as using it with a std::string would require iterating over each…
Konrad
  • 39,751
  • 32
  • 78
  • 114
972
votes
8 answers

What does %w(array) mean?

I'm looking at the documentation for FileUtils. I'm confused by the following line: FileUtils.cp %w(cgi.rb complex.rb date.rb), '/usr/lib/ruby/1.6' What does the %w mean? Can you point me to the documentation?
Dane O'Connor
  • 75,180
  • 37
  • 119
  • 173
968
votes
12 answers

Strip all non-numeric characters from string in JavaScript

Consider a non-DOM scenario where you'd want to remove all non-numeric characters from a string using JavaScript/ECMAScript. Any characters that are in range 0 - 9 should be kept. var myString = 'abc123.8'; //desired output is 1238 How would…
p.campbell
  • 98,673
  • 67
  • 256
  • 322
964
votes
10 answers

Convert hex string to integer in Python

How do I convert a hex string to an integer? "0xffff" ⟶ 65535 "ffff" ⟶ 65535
Matt
  • 84,419
  • 25
  • 57
  • 67
963
votes
13 answers

Difference between text and varchar (character varying)

What's the difference between the text data type and the character varying (varchar) data types? According to the documentation If character varying is used without length specifier, the type accepts strings of any size. The latter is a PostgreSQL…
Adam Matan
  • 128,757
  • 147
  • 397
  • 562
963
votes
17 answers

split a string on newlines in .NET

I need to split a string into newlines in .NET and the only way I know of to split strings is with the Split method. However that will not allow me to (easily) split on a newline, so what is the best way to do it?
RCIX
  • 38,647
  • 50
  • 150
  • 207
962
votes
15 answers

What are the differences between Rust's `String` and `str`?

Why does Rust have String and str? What are the differences between String and str? When does one use String instead of str and vice versa? Is one of them getting deprecated?
Daniel Fath
  • 16,453
  • 7
  • 47
  • 82
956
votes
19 answers

How to efficiently concatenate strings in go

In Go, a string is a primitive type, which means it is read-only, and every manipulation of it will create a new string. So if I want to concatenate strings many times without knowing the length of the resulting string, what's the best way to do…
Randy Sugianto 'Yuku'
  • 71,383
  • 57
  • 178
  • 228
952
votes
13 answers

Remove empty strings from a list of strings

I want to remove all empty strings from a list of strings in python. My idea looks like this: while '' in str_list: str_list.remove('') Is there any more pythonic way to do this?
zerodx
  • 9,541
  • 3
  • 16
  • 7
952
votes
16 answers

How to remove text from a string?

I've got a data-123 string. How can I remove data- from the string while leaving the 123?
Michael Grigsby
  • 11,467
  • 9
  • 33
  • 52
937
votes
13 answers

How to convert a char to a String?

I have a char and I need a String. How do I convert from one to the other?
Landon Kuhn
  • 76,451
  • 45
  • 104
  • 130
932
votes
20 answers

Converting string to byte array in C#

I'm converting something from VB into C#. Having a problem with the syntax of this statement: if ((searchResult.Properties["user"].Count > 0)) { profile.User = System.Text.Encoding.UTF8.GetString(searchResult.Properties["user"][0]); } I then…
nouptime
  • 9,929
  • 5
  • 22
  • 37
922
votes
7 answers

What is the difference between single-quoted and double-quoted strings in PHP?

I'm a little confused why I see some code in PHP with string placed in single quotes and sometimes in double quotes. I just know in .NET, or the C language, if it is in a single quote, that means it is a character, not a string.
rob waminal
  • 18,117
  • 17
  • 50
  • 64
919
votes
26 answers

Identify if a string is a number

If I have these strings: "abc" = false "123" = true "ab2" = false Is there a command, like IsNumeric() or something else, that can identify if a string is a valid number?
Gold
  • 60,526
  • 100
  • 215
  • 315
892
votes
13 answers

std::wstring VS std::string

I am not able to understand the differences between std::string and std::wstring. I know wstring supports wide characters such as Unicode characters. I have got the following questions: When should I use std::wstring over std::string? Can…
Appu