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
109
votes
6 answers

Remove part of a string

How do I remove part of a string? For example in ATGAS_1121 I want to remove everything before _.
Lisann
  • 5,705
  • 14
  • 41
  • 50
109
votes
3 answers

Get the index of a pattern in a string using regex

I want to search a string for a specific pattern. Do the regular expression classes provide the positions (indexes within the string) of the pattern within the string? There can be more that 1 occurences of the pattern. Any practical example?
Cratylus
  • 52,998
  • 69
  • 209
  • 339
109
votes
10 answers

ValueError: could not convert string to float: id

I'm running the following python script: #!/usr/bin/python import os,sys from scipy import stats import numpy as np f=open('data2.txt', 'r').readlines() N=len(f)-1 for i in range(0,N): w=f[i].split() l1=w[1:8] l2=w[8:15] …
LookIntoEast
  • 8,048
  • 18
  • 64
  • 92
109
votes
16 answers

How can I compare strings in C using a `switch` statement?

In C there is a switch construct which enables one to execute different conditional branches of code based on an test integer value, e.g., int a; /* Read the value of "a" from some source, e.g. user input */ switch (a) { case 100: // Code …
Niklas
  • 1,177
  • 2
  • 8
  • 6
109
votes
6 answers

How do I convert a Python UUID into a string?

I need to be able to assign a UUID to a user and document this in a .txt file. This is all I have: import uuid a = input("What's your name?") print(uuid.uuid1()) f.open(#file.txt) I tried: f.write(uuid.uuid1()) but nothing comes up, may be a…
Alphin Philip
  • 1,109
  • 2
  • 8
  • 6
109
votes
7 answers

string.Join on a List or other type

I want to turn an array or list of ints into a comma delimited string, like this: string myFunction(List a) { return string.Join(",", a); } But string.Join only takes List as the second parameter. What is the best way to do this?
Code Commander
  • 16,771
  • 8
  • 64
  • 65
109
votes
7 answers

Read file-contents into a string in C++

Possible Duplicate: What is the best way to slurp a file into a std::string in c++? In scripting languages like Perl, it is possible to read a file into a variable in one shot. open(FILEHANDLE,$file); $content=; What would be…
sonofdelphi
  • 1,986
  • 5
  • 19
  • 25
109
votes
5 answers

Python regex - r prefix

Can anyone explain why example 1 below works, when the r prefix is not used? I thought the r prefix must be used whenever escape sequences are used. Example 2 and example 3 demonstrate this. # example 1 import re print (re.sub('\s+', ' ', 'hello …
JT.
  • 1,099
  • 2
  • 8
  • 3
109
votes
5 answers

How can I compare a string to multiple correct values in Bash?

I have the following piece of Bash script: function get_cms { echo "input cms name" read cms cms=${cms,,} if [ "$cms" != "wordpress" && "$cms" != "meganto" && "$cms" != "typo3" ]; then get_cms fi } But no matter what I…
eagle00789
  • 1,211
  • 2
  • 8
  • 6
109
votes
6 answers

String concatenation in Jinja

I just want to loop through an existing list and make a comma delimited string out of it. Something like this: my_string = 'stuff, stuff, stuff, stuff' I already know about loop.last, I just need to know how to make the third line in my code below…
KacieHouser
  • 1,885
  • 4
  • 22
  • 35
108
votes
3 answers

How to remove symbols from a string with Python?

I'm a beginner with both Python and RegEx, and I would like to know how to make a string that takes symbols and replaces them with spaces. Any help is great. For example: how much for the maple syrup? $20.99? That's ricidulous!!! into: how much for…
aaront
  • 1,215
  • 2
  • 10
  • 10
108
votes
8 answers

How to search for a string in cell array in MATLAB?

Let's say I have the cell array strs = {'HA' 'KU' 'LA' 'MA' 'TATA'} What should I do if I want to find the index of 'KU'?
Benjamin
  • 1,223
  • 2
  • 9
  • 4
108
votes
9 answers

How to "properly" print a list?

So I have a list: ['x', 3, 'b'] And I want the output to be: [x, 3, b] How can I do this in python? If I do str(['x', 3, 'b']), I get one with quotes, but I don't want quotes.
Obaid
  • 4,328
  • 9
  • 42
  • 42
108
votes
4 answers

Replace last character of string using JavaScript

I have a very small query. I tried using concat, charAt, slice and whatnot but I didn't get how to do it. Here is my string: var str1 = "Notion,Data,Identity," I want to replace the last , with a . it should look like this. var str1 =…
Patrick
  • 3,938
  • 6
  • 20
  • 32
108
votes
6 answers

How do you pull first 100 characters of a string in PHP

I am looking for a way to pull the first 100 characters from a string variable to put in another variable for printing. Is there a function that can do this easily? For example: $string1 = "I am looking for a way to pull the first 100 characters…
JoshFinnie
  • 4,841
  • 7
  • 28
  • 28