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
105
votes
4 answers

Getting "command not found" error while comparing two strings in Bash

My whole script is currently this: #!/bin/sh clear; blanko=""; # Dummy-Variablen variable=Testvariable; if [[$variable == $blanko]]; then echo "Nichts da!" else echo $variable fi and if I enter TestSelect.sh I…
EpsilonAlpha
  • 1,153
  • 2
  • 7
  • 4
105
votes
5 answers

Regex expressions in Java, \\s vs. \\s+

What's the difference between the following two expressions? x = x.replaceAll("\\s", ""); x = x.replaceAll("\\s+", "");
mpluse
  • 1,857
  • 6
  • 18
  • 25
105
votes
14 answers

Regex for converting CamelCase to camel_case in java

I understand why the desired output is not given for converting using regex a string like FooBar to Foo_Bar which instead gives Foo_Bar_. I could have done something with String.substring substring(0, string.length() - 2) or just replaced the last…
ajmartin
  • 2,379
  • 2
  • 26
  • 42
104
votes
3 answers

split string in to 2 based on last occurrence of a separator

I would like to know if there is any built in function in python to break the string in to 2 parts, based on the last occurrence of a separator. for eg: consider the string "a b c,d,e,f" , after the split over separator ",", i want the output as "a…
Yashwanth Kumar
  • 28,931
  • 15
  • 65
  • 69
104
votes
1 answer

How do I convert between String, &str, Vec and &[u8]?

A new Rustacean like me struggles with juggling these types: String, &str, Vec, &[u8]. In time, I hope to have an epiphany and suddenly get why some library calls use one or the other. Until then, I need help to map out each idiomatic…
Martin Algesten
  • 13,052
  • 4
  • 54
  • 77
104
votes
6 answers

Java equivalent to Explode and Implode(PHP)

I am new in Java although had a good experience in PHP, and looking for perfect replacement for explode and implode (available in PHP) functions in Java. I have Googled for the same but not satisfied with the results. Anyone has the good solution…
Pankaj Wanjari
  • 1,275
  • 2
  • 9
  • 11
104
votes
17 answers

Find text in string with C#

How can I find given text within a string? After that, I'd like to create a new string between that and something else. For instance, if the string was: This is an example string and my data is here And I want to create a string with whatever is…
Wilson
  • 8,570
  • 20
  • 66
  • 101
104
votes
17 answers

Calling PHP functions within HEREDOC strings

In PHP, the HEREDOC string declarations are really useful for outputting a block of html. You can have it parse in variables just by prefixing them with $, but for more complicated syntax (like $var[2][3]), you have to put your expression inside {}…
Doug Kavendek
  • 3,624
  • 4
  • 31
  • 43
103
votes
2 answers

How to convert a String to long in javascript?

I have a millisecond timestamp that I need to convert from a string to long. JavaScript has a parseInt but not a parseLong. So how can I do this? To expand on my question slightly: given that apparently JavaScript doesn't have a long type, how can I…
Richard H
  • 38,037
  • 37
  • 111
  • 138
103
votes
17 answers

Is there a way to convert number words to Integers?

I need to convert one into 1, two into 2 and so on. Is there a way to do this with a library or a class or anything?
Llyod
103
votes
11 answers

Matching strings with wildcard

I would like to match strings with a wildcard (*), where the wildcard means "any". For example: *X = string must end with X X* = string must start with X *X* = string must contain X Also, some compound uses such as: *X*YZ* = string contains X and…
Robinson
  • 9,666
  • 16
  • 71
  • 115
103
votes
19 answers

How to check a string against null in java?

How can I check a string against null in java? I am using stringname.equalsignorecase(null) but it's not working.
user351809
  • 1,257
  • 3
  • 10
  • 7
103
votes
32 answers

The most sophisticated way for creating comma-separated Strings from a Collection/Array/List?

During my work with databases I noticed that I write query strings and in this strings I have to put several restrictions in the where-clause from a list/array/collection. Should look like this: select * from customer where customer.id in (34, 26,…
maerch
  • 2,055
  • 3
  • 17
  • 24
103
votes
6 answers

How to delete specific characters from a string in Ruby?

I have several strings that look like this: "((String1))" They are all different lengths. How could I remove the parentheses from all these strings in a loop?
Cristiano
  • 2,839
  • 8
  • 25
  • 35
103
votes
3 answers

What is the max length of a Python string?

If it is environment-independent, what is the theoretical maximum number of characters in a Python string?
citronic
  • 9,868
  • 14
  • 51
  • 74