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

How can I get a java.io.InputStream from a java.lang.String?

I have a String that I want to use as an InputStream. In Java 1.0, you could use java.io.StringBufferInputStream, but that has been @Deprecrated (with good reason--you cannot specify the character set encoding): This class does not properly…
Jared Oberhaus
  • 14,547
  • 4
  • 56
  • 55
96
votes
15 answers

Splitting CamelCase

This is all asp.net c#. I have an enum public enum ControlSelectionType { NotApplicable = 1, SingleSelectRadioButtons = 2, SingleSelectDropDownList = 3, MultiSelectCheckBox = 4, MultiSelectListBox = 5 } The numerical value of…
Robin Day
  • 100,552
  • 23
  • 116
  • 167
96
votes
6 answers

How to convert IEnumerable to one comma separated string?

Say that for debugging purposes, I want to quickly get the contents of an IEnumerable into one-line string with each string item comma-separated. I can do it in a helper method with a foreach loop, but that's neither fun nor brief. Can Linq be used?…
Johann Gerell
  • 24,991
  • 10
  • 72
  • 122
96
votes
12 answers

Case Insensitive String Comparison in C

I have two postcodes char* that I want to compare, ignoring case. Is there a function to do this? Or do I have to loop through each use the tolower function and then do the comparison? Any idea how this function will react with numbers in the…
bond425
  • 1,077
  • 1
  • 7
  • 12
96
votes
12 answers

In Javascript, how can I perform a global replace on string with a variable inside '/' and '/g'?

I want to perform a global replace of string using String.replace in Javascript. In the documentation I read that I can do this with /g, i.e. for example; var mystring = mystring.replace(/test/g, mystring); and this will replace all occurrences…
avastreg
96
votes
12 answers

swift 3.0 Data to String?

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {} I want deviceToken to string but: let str = String.init(data: deviceToken, encoding: .utf8) str is nil swift 3.0 how can I let…
weijia.wang
  • 2,088
  • 2
  • 18
  • 32
96
votes
7 answers

How to read entire stream into a std::string?

I'm trying to read an entire stream (multiple lines) into a string. I'm using this code, and it works, but it's offending my sense of style... Surely there's an easier way? Maybe using stringstreams? void Obj::loadFromStream(std::istream &…
Roddy
  • 66,617
  • 42
  • 165
  • 277
96
votes
4 answers

Split a string only by first space in python

I have string for example: "238 NEO Sports". I want to split this string only at the first space. The output should be ["238","NEO Sports"]. One way I could think of is by using split() and finally merging the last two strings returned. Is there a…
bazinga
  • 2,120
  • 4
  • 21
  • 35
96
votes
5 answers

How to remove diacritics from a String in Swift?

How to remove diacritics (or accents) from a String (like say change "één" to "een") in Swift? Do I have to go back to NSString or can it be done within Swift?
Johan Kool
  • 15,637
  • 8
  • 64
  • 81
96
votes
2 answers

What does a leading `\x` mean in a Python string `\xaa`

What is difference between 'aa' and '\xaa'? What does the \x part mean? And which chapter of the Python documentation covers this topic?
Haiyuan Zhang
  • 40,802
  • 41
  • 107
  • 134
96
votes
1 answer

How do I find the last occurrence of a substring in an NSString?

How would I get the last occurrence of an NSString within another NSString? For example, in "abc def ghi abc def ghi," I want to find the index of the second "abc," not the first. I know I could do this with a bunch of rangeOfStrings, but is there…
Debashis
  • 1,025
  • 1
  • 8
  • 10
96
votes
7 answers

Split string in JavaScript and detect line break

I have a small function I found that takes a string from a textarea and then puts it into a canvas element and wraps the text when the line gets too long. But it doesn't detect line breaks. This is what it's doing and what it should…
Dustin Silk
  • 4,320
  • 5
  • 32
  • 48
96
votes
7 answers

bash, extract string before a colon

If I have a file with rows like this /some/random/file.csv:some string /some/random/file2.csv:some string2 Is there some way to get a file that only has the first part before the colon, e.g. /some/random/file.csv /some/random/file2.csv I would…
user788171
  • 16,753
  • 40
  • 98
  • 125
96
votes
6 answers

Find the location of a character in string

I would like to find the location of a character in a string. Say: string = "the2quickbrownfoxeswere2tired" I would like the function to return 4 and 24 -- the character location of the 2s in string.
ricardo
  • 8,195
  • 7
  • 47
  • 69
96
votes
3 answers

Java: Converting String to and from ByteBuffer and associated problems

I am using Java NIO for my socket connections, and my protocol is text based, so I need to be able to convert Strings to ByteBuffers before writing them to the SocketChannel, and convert the incoming ByteBuffers back to Strings. Currently, I am…
DivideByHero
  • 19,715
  • 24
  • 57
  • 64