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
101
votes
3 answers

difference between find and index

I am new to python and cannot quite understand the difference between find and index. >>> line 'hi, this is ABC oh my god!!' >>> line.find("o") 16 >>> line.index("o") 16 They always return the same result. Thanks!!
SohamC
  • 2,367
  • 6
  • 22
  • 34
101
votes
6 answers

How to convert a string to JSON object in PHP

I have the following result from an SQL query: {"Coords":[ {"Accuracy":"65","Latitude":"53.277720488429026","Longitude":"-9.012038778269686","Timestamp":"Fri Jul 05 2013 11:59:34 GMT+0100 (IST)"}, …
user2363025
  • 6,365
  • 19
  • 48
  • 89
101
votes
5 answers

Why does "true" == true show false in JavaScript?

MDC describes the == operator as follows: If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible;…
Isaac
  • 1,677
  • 3
  • 15
  • 22
101
votes
8 answers

How can I send the "&" (ampersand) character via AJAX?

I want to send a few variables and a string with the POST method from JavaScript. I get the string from the database, and then send it to a PHP page. I am using an XMLHttpRequest object. The problem is that the string contains the character & a few…
candino
  • 1,297
  • 3
  • 11
  • 13
101
votes
8 answers

Remove a character at a certain position in a string - javascript

Is there an easy way to remove the character at a certain position in javascript? e.g. if I have the string "Hello World", can I remove the character at position 3? the result I would be looking for would the following: "Helo World" This question…
starbeamrainbowlabs
  • 5,692
  • 8
  • 42
  • 73
100
votes
5 answers

Using new line(\n) in string and rendering the same in HTML

I have a string say string display_txt = "1st line text" +"\n" + "2nd line text"; in Jquery, I am trying to use ('#somediv').html(display_txt).css("color", "green") quite clearly I am expecting my msg to be displayed in 2 lines, but instead \n is…
KeenUser
  • 5,305
  • 14
  • 41
  • 62
100
votes
5 answers

Write bytes to file

I have a hexadecimal string (e.g 0CFE9E69271557822FE715A8B3E564BE) and I want to write it to a file as bytes. For example, Offset 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 00000000 0C FE 9E 69 27 15 57 82 2F E7 15 A8 B3 E5 64 BE …
John Doe
  • 9,843
  • 13
  • 42
  • 73
100
votes
2 answers

Javascript How to get first three characters of a string

This may duplicate with previous topics but I can't find what I really need. I want to get a first three characters of a string. For example: var str = '012123'; console.info(str.substring(0,3)); //012 I want the output of this string '012' but I…
Phon Soyang
  • 1,303
  • 2
  • 9
  • 17
100
votes
9 answers

Why does C not allow concatenating strings when using the conditional operator?

The following code compiles without problems: int main() { printf("Hi" "Bye"); } However, this does not compile: int main() { int test = 0; printf("Hi" (test ? "Bye" : "Goodbye")); } What is the reason for that?
José D.
  • 4,175
  • 7
  • 28
  • 47
100
votes
8 answers

How to include quotes in a string

I have a string "I want to learn "c#"". How can I include the quotes before and after c#?
learning
  • 11,415
  • 35
  • 87
  • 154
100
votes
12 answers

Check for special characters in string

I want to check if a string contains special characters like !@#$%^&*.,<>/\'";:? and return true if the string contains atleast one of those chars. I tried with the following regex,script: var format =…
Petru Lebada
  • 2,167
  • 8
  • 38
  • 59
100
votes
2 answers

null vs empty string in Oracle

Possible Duplicate: Why does Oracle 9i treat an empty string as NULL? I have a table in Oracle 10g named TEMP_TABLE with only two columns - id and description just for the sake of demonstration. The column id is a sequence generated primary key…
Tiny
  • 27,221
  • 105
  • 339
  • 599
99
votes
8 answers

Remove last character if it's a backslash

Is there a function to chomp last character in the string if it's some special character? For example, I need to remove backslash if it's there, and do nothing, if not. I know I can do it with regex easily, but wonder if there something like a small…
Sergei Basharov
  • 51,276
  • 73
  • 200
  • 335
99
votes
9 answers

How can I replace non-printable Unicode characters in Java?

The following will replace ASCII control characters (shorthand for [\x00-\x1F\x7F]): my_string.replaceAll("\\p{Cntrl}", "?"); The following will replace all ASCII non-printable characters (shorthand for [\p{Graph}\x20]), including accented…
dagnelies
  • 5,203
  • 5
  • 38
  • 56
99
votes
17 answers

Check if String contains only letters

The idea is to have a String read and to verify that it does not contain any numeric characters. So something like "smith23" would not be acceptable.
kodie hill
  • 1,007
  • 1
  • 8
  • 3