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
119
votes
3 answers

XML Document to String

What's the simplest way to get the String representation of a XML Document (org.w3c.dom.Document)? That is all nodes will be on a single line. As an example, from trge 156 (this is only a tree representation, in my…
bluish
  • 26,356
  • 27
  • 122
  • 180
119
votes
5 answers

How do I properly use std::string on UTF-8 in C++?

My platform is a Mac. I'm a C++ beginner and working on a personal project which processes Chinese and English. UTF-8 is the preferred encoding for this project. I read some posts on Stack Overflow, and many of them suggest using std::string when…
Saddle Point
  • 3,074
  • 4
  • 23
  • 33
119
votes
8 answers

How to substring in jquery

How can I use jquery on the client side to substring "nameGorge" and remove "name" so it outputs just "Gorge"? var name = "nameGorge"; //output Gorge
Gorge
  • 1,399
  • 3
  • 9
  • 11
119
votes
7 answers

How can strings be concatenated?

How to concatenate strings in python? For example: Section = 'C_type' Concatenate it with Sec_ to form the string: Sec_C_type
michelle
  • 1,241
  • 2
  • 8
  • 4
119
votes
1 answer

How to create a Rust struct with string members?

I want the members to be owned by the struct. I'm looking for the correct declaration of a struct and instantiation examples. I wasn't able to find an example.
vladimir
  • 2,635
  • 3
  • 23
  • 26
119
votes
22 answers

Converting List to List

I have a list of integers, List and I'd like to convert all the integer objects into Strings, thus finishing up with a new List. Naturally, I could create a new List and loop through the list calling String.valueOf() for…
ChrisThomas123
  • 2,578
  • 4
  • 23
  • 20
119
votes
26 answers

Removing leading and trailing spaces from a string

How to remove spaces from a string object in C++. For example, how to remove leading and trailing spaces from the below string object. //Original string: " This is a sample string " //Desired string: "This is a sample…
Ankur
  • 11,239
  • 22
  • 63
  • 66
119
votes
5 answers

Get last "column" after .str.split() operation on column in pandas DataFrame

I have a column in a pandas DataFrame that I would like to split on a single space. The splitting is simple enough with DataFrame.str.split(' '), but I can't make a new column from the last entry. When I .str.split() the column I get a list of…
Richard Herron
  • 9,760
  • 12
  • 69
  • 116
118
votes
7 answers

What is the lifetime of the result of std::string::c_str()?

In one of my programs, I have to interface with some legacy code that works with const char*. Lets say I have a structure which looks like: struct Foo { const char* server; const char* name; }; My higher-level application only deals with…
ereOn
  • 53,676
  • 39
  • 161
  • 238
118
votes
17 answers

Manipulate a url string by adding GET parameters

I want to add GET parameters to URLs that may and may not contain GET parameters without repeating ? or &. Example: If I want to add category=action $url="http://www.acme.com"; // will add ?category=action at the…
CodeOverload
  • 47,274
  • 54
  • 131
  • 219
118
votes
13 answers

Test if string is a number in Ruby on Rails

I have the following in my application controller: def is_number?(object) true if Float(object) rescue false end and the following condition in my controller: if mystring.is_number? end The condition is throwing an undefined method error. I'm…
Jamie Buchanan
  • 3,868
  • 4
  • 22
  • 24
118
votes
3 answers

Idiomatic way to convert an InputStream to a String in Scala

I have a handy function that I've used in Java for converting an InputStream to a String. Here is a direct translation to Scala: def inputStreamToString(is: InputStream) = { val rd: BufferedReader = new BufferedReader(new…
bballant
  • 1,425
  • 2
  • 10
  • 8
118
votes
10 answers

Is string in array?

What would be the best way to look in a string[] to see if it contains a element. This was my first shot at it. But perhaps there is something that I am overlooking. The array size will be no larger than 200 elements. bool isStringInArray(string[]…
Brad
  • 20,302
  • 36
  • 84
  • 102
118
votes
8 answers

Extracting date from a string in Python

How can I extract the date from a string like "monkey 2010-07-10 love banana"? Thanks!
dmpop
  • 1,517
  • 4
  • 13
  • 10
118
votes
17 answers

Converting bool to text in C++

Maybe this is a dumb question, but is there any way to convert a boolean value to a string such that 1 turns to "true" and 0 turns to "false"? I could just use an if statement, but it would be nice to know if there is a way to do that with the…
Jason Baker
  • 192,085
  • 135
  • 376
  • 510