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:

165336 questions
7419
votes
3 answers

How to check whether a string contains a substring in JavaScript?

Usually I would expect a String.contains() method, but there doesn't seem to be one. What is a reasonable way to check for this?
gramm
  • 18,623
  • 6
  • 24
  • 25
6903
votes
62 answers

What is the difference between String and string in C#?

Example (note the case): string s = "Hello world!"; String s = "Hello world!"; What are the guidelines for the use of each? And what are the differences?
Lance Fisher
  • 25,328
  • 20
  • 91
  • 121
4813
votes
72 answers

How to replace all occurrences of a string in JavaScript

I have this string in my JavaScript code: "Test abc test test abc test test test abc test test abc" Doing: str = str.replace('abc', ''); Seems to only remove the first occurrence of abc in the string above. How can I replace all occurrences of it?
Click Upvote
  • 235,452
  • 251
  • 553
  • 736
4332
votes
60 answers

How do I read / convert an InputStream into a String in Java?

If you have a java.io.InputStream object, how should you process that object and produce a String? Suppose I have an InputStream that contains text data, and I want to convert it to a String, so for example I can write that to a log file. What is…
Johnny Maelstrom
  • 44,165
  • 5
  • 19
  • 18
4160
votes
104 answers

How do I make the first letter of a string uppercase in JavaScript?

How do I make the first letter of a string uppercase, but not change the case of any of the other letters? For example: "this is a test" → "This is a test" "the Eiffel Tower" → "The Eiffel Tower" "/index.html" → "/index.html"
Robert Wills
  • 42,187
  • 3
  • 15
  • 6
3596
votes
10 answers

Does Python have a string 'contains' substring method?

I'm looking for a string.contains or string.indexof method in Python. I want to do: if not somestring.contains("blah"): continue
Blankman
  • 236,778
  • 296
  • 715
  • 1,125
3585
votes
18 answers

Why is char[] preferred over String for passwords?

In Swing, the password field has a getPassword() (returns char[]) method instead of the usual getText() (returns String) method. Similarly, I have come across a suggestion not to use String to handle passwords. Why does String pose a threat to…
Ahamed
  • 36,657
  • 12
  • 35
  • 67
3213
votes
49 answers

How do I convert a String to an int in Java?

How can I convert a String to an int in Java? My String contains only numbers, and I want to return the number it represents. For example, given the string "1234" the result should be the number 1234.
Unknown user
  • 40,827
  • 16
  • 36
  • 40
3140
votes
78 answers

How do I iterate over the words of a string?

I'm trying to iterate over the words of a string. The string can be assumed to be composed of words separated by whitespace. Note that I'm not interested in C string functions or that kind of character manipulation/access. Also, please give…
Ashwin Nanjappa
  • 68,458
  • 72
  • 198
  • 283
3106
votes
28 answers

Case insensitive 'Contains(string)'

Is there a way to make the following return true? string title = "ASTRINGTOTEST"; title.Contains("string"); There doesn't seem to be an overload that allows me to set the case sensitivity.. Currently I UPPERCASE them both, but that's just silly (by…
Boris Callens
  • 82,870
  • 79
  • 201
  • 297
2940
votes
39 answers

Creating multiline strings in JavaScript

I have the following code in Ruby. I want to convert this code into JavaScript. What is the equivalent code in JS? text = <<"HERE" This Is A Multiline String HERE
Newy
  • 34,169
  • 9
  • 40
  • 55
2911
votes
26 answers

How to check if a string contains a substring in Bash

I have a string in Bash: string="My string" How can I test if it contains another string? if [ $string ?? 'foo' ]; then echo "It's there!" fi Where ?? is my unknown operator. Do I use echo and grep? if echo "$string" | grep 'foo'; then echo…
davidsheldon
  • 32,393
  • 4
  • 25
  • 27
2877
votes
20 answers

Convert bytes to a string

I'm using this code to get standard output from an external program: >>> from subprocess import * >>> command_stdout = Popen(['ls', '-l'], stdout=PIPE).communicate()[0] The communicate() method returns an array of bytes: >>> command_stdout b'total…
Tomas Sedovic
  • 34,313
  • 9
  • 36
  • 30
2660
votes
36 answers

How do I check if a string contains a specific word?

Consider: $a = 'How are you?'; if ($a contains 'are') echo 'true'; Suppose I have the code above, what is the correct way to write the statement if ($a contains 'are')?
Charles Yeung
  • 36,649
  • 27
  • 83
  • 130
2355
votes
14 answers

How do I get a substring of a string in Python?

Is there a way to substring a string in Python, to get a new string from the third character to the end of the string? Maybe like myString[2:end]? If leaving the second part means 'till the end', and if you leave the first part, does it start from…
Joan Venge
  • 269,545
  • 201
  • 440
  • 653
1
2 3
99 100