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
888
votes
8 answers

Print string to text file

I'm using Python to open a text document: text_file = open("Output.txt", "w") text_file.write("Purchase Amount: " 'TotalAmount') text_file.close() I want to substitute the value of a string variable TotalAmount into the text document. Can someone…
The Woo
  • 17,809
  • 26
  • 57
  • 71
884
votes
12 answers

How do you write multiline strings in Go?

Does Go have anything similar to Python's multiline strings: """line 1 line 2 line 3""" If not, what is the preferred way of writing strings spanning multiple lines?
aeter
  • 11,960
  • 6
  • 27
  • 29
882
votes
11 answers

"TypeError: a bytes-like object is required, not 'str'" when handling file content in Python 3

I've very recently migrated to Python 3.5. This code was working properly in Python 2.7: with open(fname, 'rb') as f: lines = [x.strip() for x in f.readlines()] for line in lines: tmp = line.strip().lower() if 'some-pattern' in tmp:…
masroore
  • 9,668
  • 3
  • 23
  • 28
882
votes
19 answers

How can I convert a comma-separated string to an array?

I have a comma-separated string that I want to convert into an array, so I can loop through it. Is there anything built-in to do this? For example, I have this string var str =…
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
873
votes
21 answers

How to convert string representation of list to a list

I was wondering what the simplest way is to convert a string representation of a list like the following to a list: x = '[ "A","B","C" , " D"]' Even in cases where the user puts spaces in between the commas, and spaces inside of the quotes, I need…
harijay
  • 11,303
  • 12
  • 38
  • 52
867
votes
10 answers

How to check whether a string contains a substring in Ruby

I have a string variable with content: varMessage = "hi/thsid/sdfhsjdf/dfjsd/sdjfsdn\n" "/my/name/is/balaji.so\n" "call::myFunction(int const&)\n" "void::secondFunction(char const&)\n" …
BSalunke
  • 11,499
  • 8
  • 34
  • 68
861
votes
3 answers

C# List to string with delimiter

Is there a function in C# to quickly convert some collection to string and separate values with delimiter? For example: List names --> string names_together = "John, Anna, Monica"
nan
  • 19,595
  • 7
  • 48
  • 80
855
votes
17 answers

Filter pandas DataFrame by substring criteria

I have a pandas DataFrame with a column of string values. I need to select rows based on partial string matches. Something like this idiom: re.search(pattern, cell_in_question) returning a boolean. I am familiar with the syntax of df[df['A'] ==…
euforia
  • 8,635
  • 3
  • 14
  • 5
846
votes
18 answers

How to check if a string is a substring of items in a list of strings

How do I search for items that contain the string 'abc' in the following list? xs = ['abc-123', 'def-456', 'ghi-789', 'abc-456'] The following checks if 'abc' is in the list, but does not detect 'abc-123' and 'abc-456': if 'abc' in xs:
SandyBr
  • 11,459
  • 10
  • 29
  • 27
842
votes
24 answers

Repeat a string in JavaScript a number of times

In Perl I can repeat a character multiple times using the syntax: $a = "a" x 10; // results in "aaaaaaaaaa" Is there a simple way to accomplish this in Javascript? I can obviously use a function, but I was wondering if there was any built in…
Steve
  • 53,375
  • 33
  • 96
  • 141
841
votes
46 answers

Strip HTML tags from text using plain JavaScript

How to strip off HTML tags from a string using plain JavaScript only, not using a library?
Bryan
  • 17,201
  • 24
  • 97
  • 123
838
votes
41 answers

Get the length of a String

How do you get the length of a String? For example, I have a variable defined like: var test1: String = "Scott" However, I can't seem to find a length method on the string.
Scott Walter
  • 9,426
  • 4
  • 18
  • 23
837
votes
11 answers

Split a string by another string in C#

I've been using the Split() method to split strings, but this only appears to work if you are splitting a string by a character. Is there a way to split a string, with another string being the split by parameter? I've tried converting the splitter…
Brandon
  • 8,743
  • 2
  • 20
  • 16
830
votes
41 answers

How to count string occurrence in string?

How can I count the number of times a particular string occurs in another string. For example, this is what I am trying to do in Javascript: var temp = "This is a string."; alert(temp.count("is")); //should output '2'
TruMan1
  • 33,665
  • 59
  • 184
  • 335
827
votes
31 answers

Split Strings into words with multiple word boundary delimiters

I think what I want to do is a fairly common task but I've found no reference on the web. I have text with punctuation, and I want a list of the words. "Hey, you - what are you doing here!?" should be ['hey', 'you', 'what', 'are', 'you', 'doing',…
ooboo
  • 16,259
  • 13
  • 37
  • 32