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
113
votes
20 answers

Find common substring between two strings

I'd like to compare 2 strings and keep the matched, splitting off where the comparison fails. So if I have 2 strings: string1 = "apples" string2 = "appleses" answer = "apples" Another example, as the string could have more than one word: string1 =…
NorthSide
  • 1,459
  • 2
  • 12
  • 16
113
votes
5 answers

Extract the first (or last) n characters of a string

I want to extract the first (or last) n characters of a string. This would be the equivalent to Excel's LEFT() and RIGHT(). A small example: # create a string a <- paste('left', 'right', sep = '') a # [1] "leftright" I would like to produce b, a…
Lisa Ann
  • 3,345
  • 6
  • 31
  • 42
113
votes
23 answers

Convert Variable Name to String?

I would like to convert a python variable name into the string equivalent as shown. Any ideas how? var = {} print ??? # Would like to see 'var' something_else = 3 print ??? # Would print 'something_else'
Brandon Pelfrey
  • 2,449
  • 6
  • 22
  • 22
113
votes
2 answers

How to create a numpy array of arbitrary length strings?

I'm a complete rookie to Python, but it seems like a given string is able to be (effectively) arbitrary length. i.e. you can take a string str and keeping adding to it: str += "some stuff...". Is there a way to make an array of such strings? When…
DilithiumMatrix
  • 17,795
  • 22
  • 77
  • 119
113
votes
17 answers

How to replace multiple white spaces with one white space

Let's say I have a string such as: "Hello how are you doing?" I would like a function that turns multiple spaces into one space. So I would get: "Hello how are you doing?" I know I could use regex or call string s = "Hello how…
Matt
  • 25,943
  • 66
  • 198
  • 303
113
votes
18 answers

How to test if a URL string is absolute or relative?

How can I test a URL if it is a relative or absolute path in Javascript or jQuery? I want to handle accordingly depending if the passed in URL is a local or external path. if (urlString starts with http:// or https://) //do this
TruMan1
  • 33,665
  • 59
  • 184
  • 335
112
votes
7 answers

How to get char from string by index?

Lets say I have a string that consists of x unknown chars. How could I get char nr. 13 or char nr. x-14?
TheBW
  • 1,405
  • 2
  • 11
  • 16
112
votes
11 answers

How to store phone numbers on MySQL databases?

Possible Duplicate: mysql datatype for telephne number and address Any suggestions on best practice to store telephone numbers in a DB? Consider a US phone number: 555 555 1212 555-555-1212 (555) 555 1212 5555551212 1-555-555-1212 1 (555)…
StackOverflowNewbie
  • 39,403
  • 111
  • 277
  • 441
112
votes
5 answers

Access string.xml Resource File from Java Android Code

How do you access the values in the res/values/string.xml resource file from the Android Activity class?
Ravikiran
  • 2,413
  • 5
  • 25
  • 30
112
votes
9 answers

Remove multiple spaces and new lines inside of String

Suppose we have string like this: Hello, my\n name is Michael. How can I remove that new line and strip those spaces after that into one inside of string to get this? Hello, my name is Michael.
Kreeki
  • 3,662
  • 6
  • 27
  • 33
112
votes
13 answers

Reading string from input with space character?

I'm using Ubuntu and I'm also using Geany and CodeBlock as my IDE. What I'm trying to do is reading a string (like "Barack Obama") and put it in a variable: #include int main(void) { char name[100]; printf("Enter your name: "); …
Hieu Nguyen
  • 1,497
  • 3
  • 12
  • 15
112
votes
7 answers

Ruby: Merging variables in to a string

I'm looking for a better way to merge variables into a string, in Ruby. For example if the string is something like: "The animal action the second_animal" And I have variables for animal, action and second_animal, what is the prefered way to put…
James Pearson
  • 1,622
  • 2
  • 15
  • 23
112
votes
2 answers

Converting string to numeric

I've imported a test file and tried to make a histogram pichman <- read.csv(file="picman.txt", header=TRUE, sep="/t") hist <- as.numeric(pichman$WS) However, I get different numbers from values in my dataset. Originally I thought that this …
eliavs
  • 2,306
  • 4
  • 23
  • 33
112
votes
11 answers

Why is "a" != "a" in C?

void main() { if("a" == "a") printf("Yes, equal"); else printf("No, not equal"); } Why is the output No, not equal?
Javed Akram
  • 15,024
  • 26
  • 81
  • 118
112
votes
3 answers

Opposite of String.Split with separators (.net)

Is there a way to do the opposite of String.Split in .Net? That is, to combine all the elements of an array with a given separator. Taking ["a", "b", "c"] and giving "a b c" (with a separator of " "). UPDATE: I found the answer myself. It is the…
robintw
  • 27,571
  • 51
  • 138
  • 205