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
823
votes
32 answers

Best way to strip punctuation from a string

It seems like there should be a simpler way than: import string s = "string. With. Punctuation?" # Sample string out = s.translate(string.maketrans("",""), string.punctuation) Is there?
Redwood
  • 66,744
  • 41
  • 126
  • 187
817
votes
41 answers

Split a String into an array in Swift?

Say I have a string here: var fullName: String = "First Last" I want to split the string base on white space and assign the values to their respective variables var fullNameArr = // something like: fullName.explode(" ") var firstName: String =…
blee908
  • 12,165
  • 10
  • 34
  • 41
807
votes
31 answers

How can I convert String to Int?

I have a TextBoxD1.Text and I want to convert it to an int to store it in a database. How can I do this?
turki2009
  • 8,139
  • 2
  • 19
  • 7
806
votes
8 answers

How to change a string into uppercase?

How can I convert a string into uppercase in Python? When I tried to research the problem, I found something about string.ascii_uppercase, but it couldn't solve the problem: >>> s = 'sdsd' >>> s.ascii_uppercase Traceback (most recent call last): …
gadss
  • 21,687
  • 41
  • 104
  • 154
802
votes
14 answers

Fastest method to replace all instances of a character in a string

What is the fastest way to replace all instances of a string/character in a string in JavaScript? A while, a for-loop, a regular expression?
Anriëtte Myburgh
  • 13,347
  • 11
  • 51
  • 72
800
votes
17 answers

Delete first character of string if it is 0

I want to delete the first character of a string, if the first character is a 0. The 0 can be there more than once. Is there a simple function that checks the first character and deletes it if it is 0? Right now, I'm trying it with the JS slice()…
Jings
  • 8,360
  • 3
  • 18
  • 17
800
votes
28 answers

How to replace a string in multiple files in linux command line

I need to replace a string in a lot of files in a folder, with only ssh access to the server. How can I do this?
mridul4c
  • 8,197
  • 3
  • 19
  • 28
795
votes
30 answers

In C#, should I use string.Empty or String.Empty or "" to intitialize a string?

In C#, I want to initialize a string value with an empty string. How should I do this? What is the right way, and why? string willi = string.Empty; or string willi = String.Empty; or string willi = ""; or what?
Daniel Kreiseder
  • 12,135
  • 9
  • 38
  • 59
780
votes
20 answers

How do I convert from int to String?

I'm working on a project where all conversions from int to String are done like this: int i = 5; String strI = "" + i; I'm not familiar with Java. Is this usual practice or is something wrong, as I suppose?
Denis Palnitsky
  • 18,267
  • 14
  • 46
  • 55
778
votes
39 answers

Count the number of occurrences of a character in a string in Javascript

I need to count the number of occurrences of a character in a string. For example, suppose my string contains: var mainStr = "str1,str2,str3,str4"; I want to find the count of comma , character, which is 3. And the count of individual strings after…
Akash
774
votes
22 answers

How can I do string interpolation in JavaScript?

Consider this code: var age = 3; console.log("I'm " + age + " years old!"); Are there any other ways to insert the value of a variable in to a string, apart from string concatenation?
Tom Lehman
  • 85,973
  • 71
  • 200
  • 272
774
votes
32 answers

Simple way to repeat a string

I'm looking for a simple commons method or operator that allows me to repeat some string n times. I know I could write this using a for loop, but I wish to avoid for loops whenever necessary and a simple direct method should exist somewhere. String…
Ethan Heilman
  • 16,347
  • 11
  • 61
  • 88
767
votes
17 answers

Converting a String to DateTime

How do you convert a string such as 2009-05-08 14:40:52,531 into a DateTime?
dban
766
votes
6 answers

Remove final character from string

How do I remove the last character from a string? "abcdefghij" → "abcdefghi"
user1675111
  • 8,695
  • 4
  • 18
  • 14
762
votes
4 answers

How do I strip all spaces out of a string in PHP?

How can I strip / remove all spaces of a string in PHP? I have a string like $string = "this is my string"; The output should be "thisismystring" How can I do that?
streetparade
  • 32,000
  • 37
  • 101
  • 123