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
761
votes
5 answers

Split string with multiple delimiters in Python

I found some answers online, but I have no experience with regular expressions, which I believe is what is needed here. I have a string that needs to be split by either a ';' or ', ' That is, it has to be either a semicolon or a comma followed by a…
gt565k
  • 7,755
  • 3
  • 16
  • 9
754
votes
30 answers

How do I replace a character at a particular index in JavaScript?

I have a string, let's say Hello world and I need to replace the char at index 3. How can I replace a char by specifying a index? var str = "hello world"; I need something like str.replaceAt(0,"h");
Santhosh
  • 19,616
  • 22
  • 63
  • 74
753
votes
10 answers

How to convert an int value to string in Go?

i := 123 s := string(i) s is 'E', but what I want is "123" Please tell me how can I get "123". And in Java, I can do in this way: String s = "ab" + "c" // s is "abc" how can I concat two strings in Go?
hardPass
  • 19,033
  • 19
  • 40
  • 42
750
votes
25 answers

What's the best way to convert a number to a string in JavaScript?

What's the "best" way to convert a number to a string (in terms of speed advantage, clarity advantage, memory advantage, etc) ? Some examples: String(n) n.toString() ""+n n+""
Pacerier
  • 86,231
  • 106
  • 366
  • 634
744
votes
25 answers

How can I convert a std::string to int?

I want to convert a string to an int and I don't mean ASCII codes. For a quick run-down, we are passed in an equation as a string. We are to break it down, format it correctly and solve the linear equations. Now, in saying that, I'm not able to…
Brandon
  • 7,625
  • 5
  • 19
  • 16
743
votes
9 answers

What's the @ in front of a string in C#?

This is a .NET question for C# (or possibly VB.net), but I am trying to figure out what's the difference between the following declarations: string hello = "hello"; vs. string hello_alias = @"hello"; Printing out on the console makes no…
Klaw
  • 7,519
  • 3
  • 18
  • 11
743
votes
13 answers

How do I append one string to another in Python?

How do I efficiently append one string to another? Are there any faster alternatives to: var1 = "foo" var2 = "bar" var3 = var1 + var2 For handling multiple strings in a list, see How to concatenate (join) items in a list to a single string. See…
user469652
  • 48,855
  • 59
  • 128
  • 165
739
votes
7 answers

How to find if an array contains a specific string in JavaScript/jQuery?

Can someone tell me how to detect if "specialword" appears in an array? Example: categories: [ "specialword" "word1" "word2" ]
Cofey
  • 11,144
  • 16
  • 52
  • 74
739
votes
14 answers

How can I fill out a Python string with spaces?

I want to fill out a string with spaces. I know that the following works for zero's: >>> print "'%06d'"%4 '000004' But what should I do when I want this?: 'hi ' of course I can measure string length and do str+" "*leftover, but I'd like the…
taper
  • 9,236
  • 5
  • 28
  • 29
729
votes
16 answers

Check if a string contains a string in C++

I have a variable of type std::string. I want to check if it contains a certain std::string. How would I do that? Is there a function that returns true if the string is found, and false if it isn't?
neuromancer
  • 53,769
  • 78
  • 166
  • 223
723
votes
23 answers

How do I compare strings in Java?

I've been using the == operator in my program to compare all my strings so far. However, I ran into a bug, changed one of them into .equals() instead, and it fixed the bug. Is == bad? When should it and should it not be used? What's the difference?
Nathan H
  • 48,033
  • 60
  • 165
  • 247
719
votes
11 answers

How to get the position of a character in Python?

How can I get the position of a character inside a string in Python?
user244470
  • 7,469
  • 4
  • 17
  • 10
718
votes
9 answers

Read whole ASCII file into C++ std::string

I need to read a whole file into memory and place it in a C++ std::string. If I were to read it into a char[], the answer would be very simple: std::ifstream t; int length; t.open("file.txt"); // open input file t.seekg(0, std::ios::end); //…
Escualo
  • 40,844
  • 23
  • 87
  • 135
718
votes
23 answers

Java: convert List to a join()d String

JavaScript has Array.join() js>["Bill","Bob","Steve"].join(" and ") Bill and Bob and Steve Does Java have anything like this? I know I can cobble something up myself with StringBuilder: static public String join(List list, String…
Jason S
  • 184,598
  • 164
  • 608
  • 970
707
votes
26 answers

Remove specific characters from a string in Python

I'm trying to remove specific characters from a string using Python. This is the code I'm using right now. Unfortunately, it appears to do nothing to the string. for char in line: if char in " ?.!/;:": line.replace(char,'') How do I do…
Matt Phillips
  • 11,249
  • 10
  • 46
  • 71