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

Java split string to array

I need help with the split() method. I have the followingString: String values = "0|0|0|1|||0|1|0|||"; I need to put the values into an array. There are 3 possible strings: "0", "1", and "" My problem is, when i try to use split(): String[] array…
Dusan
  • 3,284
  • 6
  • 25
  • 46
107
votes
16 answers

Get object property name as a string

Is it possible to get the object property name as a string person = {}; person.first_name = 'Jack'; person.last_name = 'Trades'; person.address = {}; person.address.street = 'Factory 1'; person.address.country = 'USA'; I'd like to use it like…
CLiFoS
  • 1,163
  • 2
  • 9
  • 11
107
votes
3 answers

String concatenation with Groovy

What is the best (idiomatic) way to concatenate Strings in Groovy? Option 1: calculateAccountNumber(bank, branch, checkDigit, account) { bank + branch + checkDigit + account } Option 2: calculateAccountNumber(bank, branch, checkDigit, account)…
Arturo Herrero
  • 12,772
  • 11
  • 42
  • 73
106
votes
10 answers

Change text color of one word in a TextView

I am looking for a way to change the color of a text of a single word in a TextView from within an Activity. For example, with this: String first = "This word is "; String next = "red" TextView t = (TextView)…
cerealspiller
  • 1,401
  • 3
  • 14
  • 23
106
votes
7 answers

replace String with another in java

What function can replace a string with another string? Example #1: What will replace "HelloBrother" with "Brother"? Example #2: What will replace "JAVAISBEST" with "BEST"?
Shah
  • 4,990
  • 10
  • 48
  • 70
106
votes
11 answers

Index of a substring in a string with Swift

I'm used to do this in JavaScript: var domains = "abcde".substring(0, "abcde".indexOf("cd")) // Returns "ab" Swift doesn't have this function, how to do something similar?
Armand Grillet
  • 3,229
  • 5
  • 30
  • 60
106
votes
2 answers

Split bash string by newline characters

I found this. And I am trying this: x='some thing' y=(${x//\n/}) And I had no luck, I thought it could work with double backslash: y=(${x//\\n/}) But it did not. To test I am not getting what I want I am doing: echo…
sites
  • 21,417
  • 17
  • 87
  • 146
106
votes
11 answers

Get the index of the nth occurrence of a string?

Unless I am missing an obvious built-in method, what is the quickest way to get the nth occurrence of a string within a string? I realize that I could loop the IndexOf method by updating its start index on each iteration of the loop. But doing it…
PeteT
  • 18,754
  • 26
  • 95
  • 132
105
votes
12 answers

How to remove the first and last character of a string?

I have worked in a SOAP message to get the LoginToken from a Webservice, and store that LoginToken as a String. U used System.out.println(LoginToken); to print the value. This prints [wdsd34svdf], but I want only wdsd34svdf. How can I remove these…
Sampath Kumar
  • 1,650
  • 4
  • 14
  • 16
105
votes
3 answers

resetting a stringstream

How do I "reset" the state of a stringstream to what it was when I created it? int firstValue = 1; int secondValue = 2; std::wstringstream ss; ss << "Hello: " << firstValue; std::wstring firstText(ss.str()); //print the value of firstText…
user974967
  • 2,928
  • 10
  • 28
  • 45
105
votes
3 answers

Prevent function taking const std::string& from accepting 0

Worth a thousand words: #include #include class SayWhat { public: SayWhat& operator[](const std::string& s) { std::cout << s << "\n"; return *this; } }; int main() { SayWhat ohNo; // ohNo[1];…
kabanus
  • 24,623
  • 6
  • 41
  • 74
105
votes
12 answers

Right way to split an std::string into a vector

What is the right way to split a string into a vector of strings? Delimiter is space or comma.
softwarematter
  • 28,015
  • 64
  • 169
  • 263
105
votes
8 answers

Can you have variables within triple quotes? If so, how?

This is probably a very simple question for some, but it has me stumped. Can you use variables within python's triple-quotes? In the following example, how do use variables in the text: wash_clothes = 'tuesdays' clean_dishes = 'never' mystring…
XL.
  • 1,390
  • 4
  • 11
  • 13
105
votes
6 answers

Is there any generic Parse() function that will convert a string to any type using parse?

I want to convert a string to a generic type like int or date or long based on the generic return type. Basically a function like Parse(String) that returns an item of type T. For example if a int was passed the function should do int.parse…
Karim
  • 6,113
  • 18
  • 58
  • 83
105
votes
4 answers

Is the time-complexity of iterative string append actually O(n^2), or O(n)?

I am working on a problem out of CTCI. The third problem of chapter 1 has you take a string such as 'Mr John Smith ' and asks you to replace the intermediary spaces with %20: 'Mr%20John%20Smith' The author offers this solution in Python, calling…