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
111
votes
6 answers

How do I find out if first character of a string is a number?

In Java is there a way to find out if first character of a string is a number? One way is string.startsWith("1") and do the above all the way till 9, but that seems very inefficient.
Omnipresent
  • 29,434
  • 47
  • 142
  • 186
110
votes
14 answers

How to check whether a str(variable) is empty or not?

How do I make a: if str(variable) == [contains text]: condition? (or something, because I am pretty sure that what I just wrote is completely wrong) I am sort of trying to check if a random.choice from my list is ["",] (blank) or contains…
user1275670
  • 1,215
  • 2
  • 8
  • 9
110
votes
9 answers

How to capitalize the first character of each word, or the first character of a whole string, with C#?

I could write my own algorithm to do it, but I feel there should be the equivalent to ruby's humanize in C#. I googled it but only found ways to humanize dates. Examples: A way to turn "Lorem Lipsum Et" into "Lorem lipsum et" A way to turn "Lorem…
marcgg
  • 65,020
  • 52
  • 178
  • 231
110
votes
15 answers

How to remove certain characters from a string in C++?

For example I have a user input a phone number. cout << "Enter phone number: "; INPUT: (555) 555-5555 cin >> phone; I want to remove the "(", ")", and "-" characters from the string. I've looked at the string remove, find and replace functions…
SD.
  • 3,089
  • 10
  • 26
  • 21
110
votes
8 answers

String-based enum in Python

To encapsulate a list of states I am using enum module: from enum import Enum class MyEnum(Enum): state1='state1' state2 = 'state2' state = MyEnum.state1 MyEnum['state1'] == state # here it works 'state1' == state # here it does not…
sophros
  • 14,672
  • 11
  • 46
  • 75
110
votes
3 answers

Finding # occurrences of a character in a string in Ruby

I'm looking for the Ruby method (1.9...) that can help me find the number of occurrences of a character in a string. I'm looking for all occurrences, not just the first one. For example: "Melanie is a noob" There are two occurrences of the letter…
Melanie Palen
  • 2,645
  • 6
  • 31
  • 50
110
votes
6 answers

Where does Java's String constant pool live, the heap or the stack?

I know the concept of a constants pool and the String constant pool used by JVMs to handle String literals. But I don't know which type of memory is used by the JVM to store String constant literals. The stack or the heap? Since its a literal which…
Rengasami Ramanujam
  • 1,858
  • 4
  • 19
  • 29
110
votes
12 answers

Convert regular Python string to raw string

I have a string s, its contents are variable. How can I make it a raw string? I'm looking for something similar to the r'' method.
rectangletangle
  • 50,393
  • 94
  • 205
  • 275
110
votes
6 answers

How do I make a new line in swift

Is there a way to have a way to make a new line in swift like "\n" for java? var example: String = "Hello World \n This is a new line"
Westink
  • 1,139
  • 2
  • 7
  • 6
110
votes
15 answers

Is it better to reuse a StringBuilder in a loop?

I've a performance related question regarding use of StringBuilder. In a very long loop I'm manipulating a StringBuilder and passing it to another method like this: for (loop condition) { StringBuilder sb = new StringBuilder(); …
Pier Luigi
  • 7,871
  • 9
  • 36
  • 46
110
votes
6 answers

How to convert an Int to Hex String in Swift

In Obj-C I used to convert an unsigned integer n to a hex string with NSString *st = [NSString stringWithFormat:@"%2X", n]; I tried for a long time to translate this into Swift language, but unsuccessfully.
boscarol
  • 1,901
  • 2
  • 15
  • 29
110
votes
3 answers

How do I test for an empty string in a Bash case statement?

I have a Bash script that performs actions based on the value of a variable. The general syntax of the case statement is: case ${command} in start) do_start ;; stop) do_stop ;; config) do_config ;; *) do_help ;; esac I'd like to…
Singlestone
  • 2,019
  • 3
  • 16
  • 18
110
votes
7 answers

Why does PHP convert a string with the letter E into a number?

Why does the following statement return true? "608E-4234" == "272E-3063" I have also tried this with single quotes around the strings. The only way I can get it to evaulate to false is by using the === operator instead of == My guess is PHP is…
Andy
  • 4,901
  • 5
  • 35
  • 57
110
votes
8 answers

Exact difference between CharSequence and String in java

I read this previous post. Can any one say what the exact difference between CharSequence and String is, other than the fact that String implements CharSequence and that String is a sequence of character? For example: CharSequence obj =…
Amith
  • 1,907
  • 5
  • 29
  • 48
110
votes
6 answers

Remove part of string after "."

I am working with NCBI Reference Sequence accession numbers like variable a: a <- c("NM_020506.1","NM_020519.1","NM_001030297.2","NM_010281.2","NM_011419.3", "NM_053155.2") To get information from the biomart package I need to remove the .1, .2…
Lisann
  • 5,705
  • 14
  • 41
  • 50