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
116
votes
17 answers

Regular Expression Match to test for a valid year

Given a value I want to validate it to check if it is a valid year. My criteria is simple where the value should be an integer with 4 characters. I know this is not the best solution as it will not allow years before 1000 and will allow years such…
Ranhiru Jude Cooray
  • 19,542
  • 20
  • 83
  • 128
116
votes
4 answers

How to sort an IEnumerable

How can I sort an IEnumerable alphabetically. Is this possible? Edit: How would I write an in-place solution?
CatZilla
  • 1,456
  • 3
  • 12
  • 13
116
votes
7 answers

How to get string width on Android?

I would like to get height too if possible.
Lindlof
  • 2,152
  • 2
  • 17
  • 26
116
votes
1 answer

Why is str.translate much faster in Python 3.5 compared to Python 3.4?

I was trying to remove unwanted characters from a given string using text.translate() in Python 3.4. The minimal code is: import sys s = 'abcde12345@#@$#%$' mapper = dict.fromkeys(i for i in range(sys.maxunicode) if chr(i) in…
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
116
votes
18 answers

Capitalize only first character of string and leave others alone? (Rails)

I'm trying to get Rails to capitalize the first character of a string, and leave all the others the way they are. I'm running into a problem where "i'm from New York" gets turned into "I'm from new york." What method would I use to select the first…
user316602
116
votes
9 answers

Swift - which types to use? NSString or String

With the introduction of Swift I've been trying to get my head round the new language I'm an iOS developer and would use types such as NSString, NSInteger, NSDictionary in an application. I've noticed that in the "The Swift Programming Language"…
Alec
  • 1,247
  • 2
  • 9
  • 9
116
votes
4 answers

How can I iterate over a string by runes in Go?

I wanted to this: for i := 0; i < len(str); i++ { dosomethingwithrune(str[i]) // takes a rune } But it turns out that str[i] has type byte (uint8) rather than rune. How can I iterate over the string by runes rather than bytes?
Matt
  • 21,026
  • 18
  • 63
  • 115
116
votes
15 answers

Convert JsonObject to String

{ "data": { "map": { "allowNestedValues": true, "create": "2012-12-11 15:16:13", "title": "test201212110004", "transitions": [] } }, "msg": "success", …
Jay Zhang
  • 1,195
  • 2
  • 7
  • 3
116
votes
10 answers

Replace Multiple String Elements in C#

Is there a better way of doing this... MyString.Trim().Replace("&", "and").Replace(",", "").Replace(" ", " ") .Replace(" ", "-").Replace("'", "").Replace("/", "").ToLower(); I've extended the string class to keep it down to one job but…
Chris McKee
  • 4,298
  • 10
  • 48
  • 83
116
votes
22 answers

How to get a string between two characters?

I have a string, String s = "test string (67)"; I want to get the no 67 which is the string between ( and ). Can anyone please tell me how to do this?
Roshanck
  • 2,220
  • 8
  • 41
  • 56
115
votes
16 answers

How do I interpolate strings?

I want to do the following in C# (coming from a Python background): strVar = "stack" mystr = "This is %soverflow" % (strVar) How do I replace the token inside the string with the value outside of it?
sazr
  • 24,984
  • 66
  • 194
  • 362
115
votes
3 answers

How is String concatenation implemented in Java 9?

As written in JEP 280: Indify String Concatenation: Change the static String-concatenation bytecode sequence generated by javac to use invokedynamic calls to JDK library functions. This will enable future optimizations of String concatenation…
Mohit Tyagi
  • 2,788
  • 4
  • 17
  • 29
115
votes
30 answers

Convert string with dashes to camelCase

I want to take a string like this: 'this-is-a-string' and convert it to this: 'thisIsAString': function dashesToCamelCase($string, $capitalizeFirstCharacter = false) { // Do stuff return $string; } I need to convert "kebab-case" to…
Kirk Ouimet
  • 27,280
  • 43
  • 127
  • 177
115
votes
22 answers

Convert String to Float in Swift

I'm trying to convert numbers taken from a UITextField, which I presume, are actually Strings, and convert them to Float, so I can multiply them. I have two UITextfields which are declared as follows: @IBOutlet var wage: UITextField @IBOutlet var…
Stephen Fox
  • 14,190
  • 19
  • 48
  • 52
115
votes
5 answers

String formatting in Python 3

I do this in Python 2: "(%d goals, $%d)" % (self.goals, self.penalties) What is the Python 3 version of this? I tried searching for examples online but I kept getting Python 2 versions.
JoseBazBaz
  • 1,445
  • 4
  • 14
  • 23