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

Scala check if element is present in a list

I need to check if a string is present in a list, and call a function which accepts a boolean accordingly. Is it possible to achieve this with a one liner? The code below is the best I could get: val strings = List("a", "b", "c") val myString =…
Dario Oddenino
  • 1,294
  • 2
  • 9
  • 15
103
votes
5 answers

Android: html in strings.xml

I would like display for example this html code:

Hello World

This is a test of the URL Example

This text is bold

This text is…

Alberto Alberto
  • 1,031
  • 2
  • 8
  • 6
103
votes
3 answers

How can I safely convert a byte array into a string and back?

I don't really care about encoding and stuff, as long as I get back the exact same byte array. So to sum up: How do I convert a byte array into a string, and then that string back into the same byte array I started with?
Svish
  • 152,914
  • 173
  • 462
  • 620
103
votes
3 answers

Passing std::string by Value or Reference

Possible Duplicate: Are the days of passing const std::string & as a parameter over? Should I pass std::string by value or by reference (to a un-inlined function) if move semantics is supported? And what about implementations using small string…
Nordlöw
  • 11,838
  • 10
  • 52
  • 99
103
votes
5 answers

Python: find closest string (from a list) to another string

Let's say I have a string "Hello" and a list words = ['hello', 'Hallo', 'hi', 'house', 'key', 'screen', 'hallo','question', 'Hallo', 'format'] How can I find the n words that are the closest to "Hello" and present in the list words ? In this case,…
Laura
  • 4,199
  • 6
  • 23
  • 24
102
votes
4 answers

Sorting Characters Of A C++ String

If i have a string is there a built in function to sort the characters or would I have to write my own? for example: string word = "dabc"; I would want to change it so that: string sortedWord = "abcd"; Maybe using char is a better option? How…
gprime
  • 2,283
  • 7
  • 38
  • 50
102
votes
10 answers

How can non-ASCII characters be removed from a string?

I have strings "A função", "Ãugent" in which I need to replace characters like ç, ã, and à with empty strings. How can I remove those non-ASCII characters from my string? I have attempted to implement this using the following function, but it is not…
rahulsri
  • 2,711
  • 4
  • 16
  • 11
102
votes
4 answers

If Python strings are immutable, why does it keep the same id if I use += to append to it?

Strings in Python are immutable, which means the value cannot be changed. However, when appending to the string in the following example, it looks like the original string memory is modified since the id remains the same: >>> s = 'String' >>> for i…
Master_Roshy
  • 1,245
  • 3
  • 12
  • 19
102
votes
3 answers

PHP: convert spaces in string into %20?

How can I convert spaces in string into %20? Here is my attempt: $str = "What happens here?"; echo urlencode($str); The output is "What+happens+here%3F", so the spaces are not represented as %20. What am I doing wrong?
matt
  • 42,713
  • 103
  • 264
  • 397
102
votes
0 answers

C# split string but keep split chars / separators

I'm splitting a string by three different characters but I want the output to include the characters I split by. Is there any easy way to do this?
cody
102
votes
5 answers

Replacing a character from a certain index

How can I replace a character in a string from a certain index? For example, I want to get the middle character from a string, like abc, and if the character is not equal to the character the user specifies, then I want to replace it. Something like…
Jordan Baron
  • 3,752
  • 4
  • 15
  • 26
102
votes
4 answers

Remove Last Path Component In a String

I have a path: myPath = "C:\Users\myFile.txt" I would like to remove the end path so that the string only contains: "C:\Users" So far I am using split, but it just gives me a list, and im stuck at this point. myPath = myPath.split(os.sep)
Brock Woolf
  • 46,656
  • 50
  • 121
  • 144
102
votes
2 answers

How to get the last X Characters of a Golang String?

If I have the string "12121211122" and I want to get the last 3 characters (e.g. "122"), is that possible in Go? I've looked in the string package and didn't see anything like getLastXcharacters.
sourcey
  • 2,481
  • 5
  • 16
  • 13
102
votes
3 answers

Check if string has date, any format

How do I check if a string can be parsed to a date? Jan 19, 1990 January 19, 1990 Jan 19,1990 01/19/1990 01/19/90 1990 Jan 1990 January1990 These are all valid dates. If there's any concern regarding the lack of space in between stuff in item #3…
zack_falcon
  • 4,186
  • 20
  • 62
  • 108
102
votes
9 answers

Upper vs Lower Case

When doing case-insensitive comparisons, is it more efficient to convert the string to upper case or lower case? Does it even matter? It is suggested in this SO post that C# is more efficient with ToUpper because "Microsoft optimized it that way." …
Parappa
  • 7,566
  • 3
  • 34
  • 38