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

How to convert a std::string to const char* or char*

How can I convert an std::string to a char* or a const char*?
user37875
  • 13,904
  • 9
  • 37
  • 43
1048
votes
1 answer

Escape curly brace '{' in String.Format

How do I display a literal curly brace character when using the String.Format method? Example: sb.AppendLine(String.Format("public {0} {1} { get; private set; }", prop.Type, prop.Name)); I would like the output to look like this: public Int32…
PhilB
  • 11,732
  • 2
  • 19
  • 15
1047
votes
14 answers

Why can't I use switch statement on a String?

Is this functionality going to be put into a later Java version? Can someone explain why I can't do this, as in, the technical way Java's switch statement works?
Alex Beardsley
  • 20,988
  • 15
  • 52
  • 67
1047
votes
14 answers

How to get a function name as a string?

How do I get a function's name as a string? def foo(): pass >>> name_of(foo) "foo"
X10
  • 17,155
  • 7
  • 30
  • 28
1044
votes
24 answers

Creating a comma separated list from IList or IEnumerable

What is the cleanest way to create a comma-separated list of string values from an IList or IEnumerable? String.Join(...) operates on a string[] so can be cumbersome to work with when types such as IList or…
Daniel Fortunov
  • 43,309
  • 26
  • 81
  • 106
1044
votes
41 answers

How to check if a String is numeric in Java

How would you check if a String was a number before parsing it?
Craig Angus
  • 22,784
  • 18
  • 55
  • 63
1033
votes
35 answers

How would you count occurrences of a char within a string?

I am doing something where I realised I wanted to count how many /s I could find in a string, and then it struck me, that there were several ways to do it, but couldn't decide on what the best (or easiest) was. At the moment I'm going with something…
Ian G
  • 29,468
  • 21
  • 78
  • 92
1028
votes
23 answers

.NET String.Format() to add commas in thousands place for a number

I want to add a comma in the thousands place for a number. Would String.Format() be the correct path to take? What format would I use?
Seibar
  • 68,705
  • 38
  • 88
  • 99
1021
votes
27 answers

Remove empty array elements

Some elements in my array are empty strings from users. $linksArray still has empty elements after the following: foreach($linksArray as $link) { if($link == '') { unset($link); } } print_r($linksArray); The empty() function…
Will
  • 10,731
  • 6
  • 22
  • 16
1014
votes
22 answers

How to check if type of a variable is string?

Is there a way to check if the type of a variable in python is a string, like: isinstance(x,int); for integer values?
c_pleaseUpvote
  • 10,173
  • 3
  • 15
  • 6
1012
votes
21 answers

Best way to repeat a character in C#

What is the best way to generate a string of \t's in C#? I am learning C# and experimenting with different ways of saying the same thing. Tabs(uint t) is a function that returns a string with t amount of \t's For example, Tabs(3) returns…
Alex Baranosky
  • 48,865
  • 44
  • 102
  • 150
1011
votes
5 answers

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

Given a string: String exampleString = "example"; How do I convert it to an InputStream?
Iain
  • 10,433
  • 16
  • 56
  • 62
1004
votes
17 answers

Java string to date conversion

What is the best way to convert a String in the format 'January 2, 2010' to a Date in Java? Ultimately, I want to break out the month, the day, and the year as integers so that I can use Date date = new…
littleK
  • 19,521
  • 30
  • 128
  • 188
1003
votes
69 answers

PHP random string generator

I'm trying to create a randomized string in PHP, and I get absolutely no output with this:
Captain Lightning
  • 10,493
  • 4
  • 19
  • 17
994
votes
14 answers

How do I generate a stream from a string?

I need to write a unit test for a method that takes a stream which comes from a text file. I would like to do do something like this: Stream s = GenerateStreamFromString("a,b \n c,d");
Omu
  • 69,856
  • 92
  • 277
  • 407