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
98
votes
11 answers

How do I verify that a string only contains letters, numbers, underscores and dashes?

I know how to do this if I iterate through all of the characters in the string but I am looking for a more elegant method.
Ethan Post
  • 3,020
  • 3
  • 27
  • 27
98
votes
10 answers

Parse String to Date with Different Format in Java

I want to convert String to Date in different formats. For example, I am getting from user, String fromDate = "19/05/2009"; // i.e. (dd/MM/yyyy) format I want to convert this fromDate as a Date object of "yyyy-MM-dd" format How can I do this?
Gnaniyar Zubair
  • 8,114
  • 23
  • 61
  • 72
98
votes
5 answers

Convert a space delimited string to list

i have a string like this : states = "Alaska Alabama Arkansas American Samoa Arizona California Colorado" and I want to split it into a list like this states = {Alaska, Alabama, Arkansas, American, Samoa, ....} I am new in python. Help me, please.…
Hudec
  • 1,001
  • 2
  • 8
  • 3
98
votes
4 answers

PHP remove all characters before specific string

I need to remove all characters from any string before the occurrence of this inside the string: "www/audio" Not sure how I can do this.
user547794
  • 14,263
  • 36
  • 103
  • 152
98
votes
15 answers

Easy way of finding decimal places

Is there an easy way or integrated function to find out the decimal places of a floating point number? The number is parsed from a string, so one way is to count the digits after the . sign, but that looks quite clumsy to me. Is there a possibility…
Constantinius
  • 34,183
  • 8
  • 77
  • 85
98
votes
5 answers

String.Join vs. StringBuilder: which is faster?

In a previous question about formatting a double[][] to CSV format, it was suggested that using StringBuilder would be faster than String.Join. Is this true?
Hosam Aly
  • 41,555
  • 36
  • 141
  • 182
98
votes
2 answers

How to convert Set to string with space?

I want to convert JavaScript Set to string with space. For example, if I have a set like: var foo = new Set(); foo.add('hello'); foo.add('world'); foo.add('JavaScript'); And I'd like to print the string from the set: hello world JavaScript (space…
KimchiMan
  • 4,836
  • 6
  • 35
  • 41
98
votes
9 answers

String concatenation vs. string substitution in Python

In Python, the where and when of using string concatenation versus string substitution eludes me. As the string concatenation has seen large boosts in performance, is this (becoming more) a stylistic decision rather than a practical one? For a…
gotgenes
  • 38,661
  • 28
  • 100
  • 128
98
votes
5 answers

Are ES6 template literals faster than string concatenation?

Does HTML code generation run measurably faster in modern browsers when using string concatenation or template literals in ES6? For example: String concatenation ""+ "
"+ "
hurrymaplelad
  • 26,645
  • 10
  • 56
  • 76
98
votes
9 answers

How do I remove all specific characters at the end of a string in PHP?

How do I remove the last character only if it's a period? $string = "something here."; $output = 'something here';
ggggggggg
  • 1,179
  • 1
  • 8
  • 9
98
votes
7 answers

Remove empty strings from array while keeping record Without Loop?

This question was asked here: Remove empty strings from array while keeping record of indexes with non empty strings If you'd notice the given as @Baz layed it out; "I", "am", "", "still", "here", "", "man" "and from this I wish to produce the…
Universal Grasp
  • 1,835
  • 3
  • 20
  • 29
98
votes
10 answers

Finding second occurrence of a substring in a string in Java

We are given a string, say, "itiswhatitis" and a substring, say, "is". I need to find the index of 'i' when the string "is" occurs a second time in the original string. String.indexOf("is") will return 2 in this case. I want the output to be 10 in…
AmanArora
  • 2,379
  • 6
  • 19
  • 22
98
votes
6 answers

How do I convert a String to a BigInteger?

I'm trying to read some really big numbers from standard input and add them together. However, to add to BigInteger, I need to use BigInteger.valueOf(long);: private BigInteger sum = BigInteger.valueOf(0); private void sum(String newNumber) { …
Twinone
  • 2,949
  • 4
  • 28
  • 39
98
votes
5 answers

C - The %x format specifier

I have a small question. I know that the %x format specifier can be used to read values from the stack in a format string attack. I found the following code: %08x%08x%08x%08x What does the 08 mean? What is it doing exactly? Thanks :)
Matthew
  • 4,477
  • 21
  • 70
  • 93
98
votes
7 answers

Trim string's suffix or extension?

For example, I have a string, consists of "sample.zip". How do I remove the ".zip" extension using strings package or other else?
Coder
  • 1,923
  • 4
  • 18
  • 18