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
115
votes
2 answers

Qt c++ aggregate 'std::stringstream ss' has incomplete type and cannot be defined

I have this function in my program that converts integers to strings: QString Stats_Manager::convertInt(int num) { stringstream ss; ss << num; return ss.str(); } But when ever i run this i get the…
tyty5949
  • 1,480
  • 5
  • 14
  • 17
114
votes
3 answers

Why does substring slicing with index out of range work?

Why doesn't 'example'[999:9999] result in error? Since 'example'[9] does, what is the motivation behind it? From this behavior I can assume that 'example'[3] is, essentially/internally, not the same as 'example'[3:4], even though both result in the…
ijverig
  • 2,795
  • 3
  • 18
  • 26
114
votes
10 answers

How to determine whether a substring is in a different string

I have a sub-string: substring = "please help me out" I have another string: string = "please help me out so that I could solve this" How do I find if substring is a subset of string using Python?
sunny
  • 1,143
  • 2
  • 7
  • 4
114
votes
4 answers

How can I iterate through the unicode codepoints of a Java String?

So I know about String#codePointAt(int), but it's indexed by the char offset, not by the codepoint offset. I'm thinking about trying something like: using String#charAt(int) to get the char at an index testing whether the char is in the…
rampion
  • 87,131
  • 49
  • 199
  • 315
113
votes
5 answers

Return first match of Ruby regex

I'm looking for a way to perform a regex match on a string in Ruby and have it short-circuit on the first match. The string I'm processing is long and from what it looks like the standard way (match method) would process the whole thing, collect…
Daniel Beardsley
  • 19,907
  • 21
  • 66
  • 79
113
votes
10 answers

How to check if a string starts with another string in C?

Is there something like startsWith(str_a, str_b) in the standard C library? It should take pointers to two strings that end with nullbytes, and tell me whether the first one also appears completely at the beginning of the second…
thejh
  • 44,854
  • 16
  • 96
  • 107
113
votes
10 answers

How to replace special characters in a string?

I have a string with lots of special characters. I want to remove all those, but keep alphabetical characters. How can I do this?
Tanu
  • 1,155
  • 2
  • 9
  • 5
113
votes
15 answers

Subscript and Superscript a String in Android

How can you print a string with a subscript or superscript? Can you do this without an external library? I want this to display in a TextView in Android.
Mohit Deshpande
  • 53,877
  • 76
  • 193
  • 251
113
votes
2 answers

How to check what a String starts with (prefix) or ends with (suffix) in Swift

I'm trying to test if a Swift string starts or ends with a certain value. These methods do not exist: var str = "Hello, playground" str.startsWith("Hello") // error str.endsWith("ground") // error I would also like to get the prefix and suffix…
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
113
votes
1 answer

How to convert a &str to a &[u8]

This seems trivial, but I cannot find a way to do it. For example, fn f(s: &[u8]) {} pub fn main() { let x = "a"; f(x) } Fails to compile with: error: mismatched types: expected `&[u8]`, found `&str` (expected slice, found str)…
ynimous
  • 4,642
  • 6
  • 27
  • 43
113
votes
17 answers

How to compare two strings ignoring case in Swift language?

How can we compare two strings in swift ignoring case ? for eg : var a = "Cash" var b = "cash" Is there any method that will return true if we compare var a & var b
ak_tyagi
  • 1,552
  • 2
  • 12
  • 20
113
votes
19 answers

Can I multiply strings in Java to repeat sequences?

I have something like the following: int i = 3; String someNum = "123"; I'd like to append i "0"s to the someNum string. Does it have some way I can multiply a string to repeat it like Python does? So I could just go: someNum = sumNum + ("0" *…
Mithrax
  • 7,603
  • 18
  • 55
  • 60
113
votes
6 answers

Split string based on the first occurrence of the character

How can I split a C# string based on the first occurrence of the specified character? Suppose I have a string with value: 101,a,b,c,d I want to split it as 101 a,b,c,d That is by the first occurrence of comma character.
Vishnu Y
  • 2,211
  • 4
  • 25
  • 38
113
votes
7 answers

What is the proper way to check if a string is empty in Perl?

I have been using this code to check if a string is empty: if ($str == "") { // ... } And also the opposite with the not equals operator... if ($str != "") { // ... } This seems to work (I think), but I'm not sure it's the correct way, or if…
Nick Bolton
  • 38,276
  • 70
  • 174
  • 242
113
votes
3 answers

MySql - Way to update portion of a string?

I'm looking for a way to update just a portion of a string via MySQL query. For example, if I have 10 records all containing string as part of the field value, is there a way to change string to anothervalue for each row via one query? I.e. for the…
n00b0101
  • 6,863
  • 17
  • 42
  • 36