A "string" is a sequence of characters typically representing a unit of human-readable text. Questions on this topic deal with processing strings in programs, and how various languages and environments define and manipulate strings.
Questions tagged [strings]
201 questions
96
votes
5 answers
Why is %s better than + for concatenation?
I understand that we should use %s to concatenate a string rather than + in Python.
I could do any of:
hello = "hello"
world = "world"
print hello + " " + world
print "%s %s" % (hello, world)
print "{} {}".format(hello, world)
print '…

Niklas Rosencrantz
- 8,008
- 17
- 56
- 95
85
votes
11 answers
Why is String immutable in Java?
I couldn't understand the reason of it. I always use String class like other developers, but when I modify the value of it, new instance of String created.
What might be the reason of immutability for String class in Java?
I know there are some…

yfklon
- 1,752
- 4
- 19
- 30
81
votes
12 answers
SQL: empty string vs NULL value
I know this subject is a bit controversial and there are a lot of various articles/opinions floating around the internet. Unfortunatelly, most of them assume the person doesn't know what the difference between NULL and empty string is. So they tell…

Jacek Prucia
- 2,264
- 1
- 17
- 15
70
votes
3 answers
Etymology of "String"
So it's obvious that a string of things is a sequence of things, and so a sequence of characters/bytes/etc. might as well be called a string. But who first called them strings? And when? And in what context such that it stuck around? I've always…

sclv
- 1,070
- 7
- 11
52
votes
6 answers
Is initializing a char[] with a string literal bad practice?
I was reading a thread titled "strlen vs sizeof" on CodeGuru, and one of the replies states that "it's anyways [sic] bad practice to initialie [sic] a char array with a string literal."
Is this true, or is that just his (albeit an "elite member")…

Cole Tobin
- 1,440
- 1
- 15
- 28
46
votes
2 answers
Why store flags/enums in a database as strings instead of integers?
I've been browsing SQL dumps of some famous CMSes, including Drupal 7, Wordpress (some quite very old version), and some custom application based on Python.
All of these dumps contained data with string flags instead of integer ones. For example, a…

trejder
- 2,386
- 3
- 19
- 39
43
votes
1 answer
C++11 includes std::stoi, why not std::itos?
I noticed to my glee that C++11 has a std::sto@ family of functions for easily unpacking ints/floats/longs whatever from strings. I'm surprised however, that the opposite isn't implemented. Why didn't the standards committee include a std::itos…

Doug T.
- 11,642
- 5
- 43
- 69
40
votes
6 answers
Hardcoding strings that will never change
So, in my efforts to write a program to conjugate verbs (algorithmically, not through a dataset) for French, I've come across a slight problem.
The algorithm to conjugate the verbs is actually fairly simple for the 17-or-so cases of verbs, and runs…

Chris Cirefice
- 2,984
- 4
- 17
- 37
36
votes
2 answers
Why are C string literals read-only?
What advantage(s) of string literals being read-only justify(-ies/-ied) the:
Yet another way to shoot yourself in the foot
char *foo = "bar";
foo[0] = 'd'; /* SEGFAULT */
Inability to elegantly initialize a read-write array of words in one…
user173341
34
votes
3 answers
Which string search algorithm is actually the fastest?
I have been stuck for some time on which is the fastest string search algorithm, heard many opinions, but in the end I'm not sure.
I have heard some people saying that the fastest algorithm is Boyer-Moore and some saying that Knuth-Morris-Pratt is…

vandamon taigi
- 361
- 1
- 3
- 7
31
votes
2 answers
Why does Java use UTF-16 for internal string representation?
I would imagine the reason was fast, array like access to the character at index, but some characters won't fit into 16 bits, so it wouldn't work...
So if you have to handle special cases anyways, why not just use UTF-8?

zduny
- 2,623
- 2
- 19
- 24
30
votes
3 answers
When should I use string_view in an interface?
I'm using an internal library that was designed to mimic a proposed C++ library, and sometime in the past few years I see its interface changed from using std::string to string_view.
So I dutifully change my code, to conform to the new interface.…

T.E.D.
- 1,069
- 1
- 9
- 11
30
votes
6 answers
Should a string constant be defined if it's only going to be used once?
We're implementing an adapter for Jaxen (an XPath library for Java) that allows us to use XPath to access the data model of our application.
This is done by implementing classes which map strings (passed to us from Jaxen) into elements of our data…

gutch
- 520
- 1
- 4
- 10
24
votes
4 answers
How can I extract words from a sentence and determine what part of speech each is?
I want to write something that takes a sentence and identifies each word it contains and defines what part of speech each word is.
For example
Hello World, I am a sentence
would return this
verb noun, pronoun verb adjective noun
Ideally, I'd…

Vinny
- 259
- 1
- 2
- 5
22
votes
5 answers
Why are strings so slow?
Ever since my very first programming class in high school, I've been hearing that string operations are slower — i.e. more costly — than the mythical "average operation." Why makes them so slow? (This question left intentionally broad.)

Pops
- 4,093
- 4
- 28
- 41