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
99
votes
4 answers

Convert std::string to QString

I've got an std::string content that I know contains UTF-8 data. I want to convert it to a QString. How do I do that, avoiding the from-ASCII conversion in Qt?
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
99
votes
2 answers

Difference between and

I noticed that there was (at least on Mac OS X) both a header and a header. man 3 string reveals that they contain different functions. Is there any reason for this?
icktoofay
  • 126,289
  • 21
  • 250
  • 231
99
votes
19 answers

Finding multiple occurrences of a string within a string in Python

How do I find multiple occurrences of a string within a string in Python? Consider this: >>> text = "Allowed Hello Hollow" >>> text.find("ll") 1 >>> So the first occurrence of ll is at 1 as expected. How do I find the next occurrence of it? Same…
user225312
  • 126,773
  • 69
  • 172
  • 181
99
votes
4 answers

Can Golang multiply strings like Python can?

Python can multiply strings like so: Python 3.4.3 (default, Mar 26 2015, 22:03:40) [GCC 4.9.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>> x = 'my new text is this long' >>> y = '#' * len(x) >>>…
Duke Dougal
  • 24,359
  • 31
  • 91
  • 123
99
votes
16 answers

Tetris-ing an array

Consider the following array: /www/htdocs/1/sites/lib/abcdedd /www/htdocs/1/sites/conf/xyz /www/htdocs/1/sites/conf/abc/def /www/htdocs/1/sites/htdocs/xyz /www/htdocs/1/sites/lib2/abcdedd what is the shortest and most elegant way of detecting the…
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
99
votes
8 answers

How to get ° character in a string in python?

How can I get a ° (degree) character into a string?
Richard
  • 15,152
  • 31
  • 85
  • 111
99
votes
3 answers

jQuery removing '-' character from string

I have a string "-123445". Is it possible to remove the '-' character from the string? I have tried the following but to no avail: $mylabel.text("-123456"); $mylabel.text().replace('-', '');
Riain McAtamney
  • 6,342
  • 17
  • 49
  • 62
99
votes
9 answers

How can I convert a string to a number in Perl?

I have a string which holds a decimal value in it and I need to convert that string into a floating point variable. So an example of the string I have is "5.45" and I want a floating point equivalent so I can add .1 to it. I have searched around the…
Anton
  • 12,285
  • 20
  • 64
  • 84
99
votes
21 answers

How to split filename from file extension in Swift?

Given the name of a file in the bundle, I want load the file into my Swift app. So I need to use this method: let soundURL = NSBundle.mainBundle().URLForResource(fname, withExtension: ext) For whatever reason, the method needs the filename…
JohnK
  • 6,865
  • 8
  • 49
  • 75
99
votes
13 answers

Swift - Remove " character from string

I have a string which is "Optional("5")". I need to remove the "" surrounding the 5. I have removed the Optional by doing: text2 = text2.stringByReplacingOccurrencesOfString("Optional(", withString: "", options:…
Calan Williams
  • 1,023
  • 1
  • 7
  • 6
99
votes
14 answers

How do I compare two strings in python if order does not matter?

I have two strings like string1="abc def ghi" and string2="def ghi abc" How to get that this two string are same without breaking the words?
user3064366
  • 1,509
  • 2
  • 11
  • 15
99
votes
9 answers

How can I emulate a mutable string in Python (like StringBuffer in Java or StringBuilder in C#)?

Since Python's strings are immutable, it's inefficient to edit them repeatedly in loops. How can I use a mutable data structure to implement string operations, so as to avoid making lots of temporary strings?
user2902773
  • 991
  • 1
  • 6
  • 4
99
votes
6 answers

How can I prevent java.lang.NumberFormatException: For input string: "N/A"?

While running my code I am getting a NumberFormatException: java.lang.NumberFormatException: For input string: "N/A" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at…
codemaniac143
  • 1,241
  • 2
  • 11
  • 18
99
votes
7 answers

Current date and time as string

I wrote a function to get a current date and time in format: DD-MM-YYYY HH:MM:SS. It works but let's say, its pretty ugly. How can I do exactly the same thing but simpler? string currentDateToString() { time_t now = time(0); tm *ltm =…
Katie
  • 3,517
  • 11
  • 36
  • 49
99
votes
5 answers

Converting string to double in C#

I have a long string with double-type values separated by # -value1#value2#value3# etc I splitted it to string table. Then, I want to convert every single element from this table to double type and I get an error. What is wrong with type-conversion…
whoah
  • 4,363
  • 10
  • 51
  • 81