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

Convert a file path to Uri in Android

I have an app where I capture a video using the camera. I can get the video's file path, but I need it as a Uri. The file path I'm getting: /storage/emulated/0/DCIM/Camera/20141219_133139.mp4 What I need is like…
Vinodh Kumar
  • 1,457
  • 2
  • 10
  • 17
118
votes
12 answers

How do I make my string comparison case-insensitive?

I created a Java program to compare two strings: String str = "Hello"; if (str.equals("hello")) { System.out.println("match"); } else { System.out.println("no match"); } It's case-sensitive. How can I change it so that it's not?
user268018
  • 1,245
  • 2
  • 9
  • 7
118
votes
9 answers

Extracting the last n characters from a ruby string

To get the last n characters from a string, I assumed you could use ending = string[-n..-1] but if the string is less than n letters long, you get nil. What workarounds are available? Background: The strings are plain ASCII, and I have access to…
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
118
votes
2 answers

Python string interning

While this question doesn't have any real use in practice, I am curious as to how Python does string interning. I have noticed the following. >>> "string" is "string" True This is as I expected. You can also do this. >>> "strin"+"g" is…
Ze'ev G
  • 1,646
  • 2
  • 12
  • 15
118
votes
15 answers

Read string from .resx file in C#

How can I read a string from a .resx file in C#?
Red Swan
  • 15,157
  • 43
  • 156
  • 238
117
votes
6 answers

How many String objects will be created when using a plus sign?

How many String objects will be created when using a plus sign in the below code? String result = "1" + "2" + "3" + "4"; If it was as below, I would have said three String objects: "1", "2", "12". String result = "1" + "2"; I also know that String…
The Light
  • 26,341
  • 62
  • 176
  • 258
117
votes
11 answers

How to count the number of lines of a string in javascript

I would like to count the number of lines in a string. I tried to use this stackoverflow answer, lines = str.split("\r\n|\r|\n"); return lines.length; on this string (which was originally a buffer): GET / HTTP/1.1 Host: localhost:8888 …
Itzik984
  • 15,968
  • 28
  • 69
  • 107
117
votes
5 answers

Get the first character of the first string in a list?

How would I get the first character from the first string in a list in Python? It seems that I could use mylist[0][1:] but that does not give me the first character. >>> mylist = [] >>> mylist.append("asdf") >>> mylist.append("jkl;") >>>…
Trcx
  • 4,164
  • 6
  • 30
  • 30
117
votes
6 answers

javascript regular expression to not match a word

How do I use a javascript regular expression to check a string that does not match certain words? For example, I want a function that, when passed a string that contains either abc or def, returns false. 'abcd' -> false 'cdef' -> false 'bcd' ->…
bxx
  • 1,701
  • 2
  • 12
  • 16
117
votes
19 answers

How do you convert a byte array to a hexadecimal string in C?

I have: uint8 buf[] = {0, 1, 10, 11}; I want to convert the byte array to a string such that I can print the string using printf: printf("%s\n", str); and get (the colons aren't necessary): "00:01:0A:0B" Any help would be greatly appreciated.
Steve Walsh
  • 6,363
  • 12
  • 42
  • 54
117
votes
14 answers

Most pythonic way to interleave two strings

What's the most pythonic way to mesh two strings together? For example: Input: u = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' l = 'abcdefghijklmnopqrstuvwxyz' Output: 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
Brandon Deo
  • 4,195
  • 4
  • 25
  • 42
117
votes
17 answers

How many spaces will Java String.trim() remove?

In Java, I have a String like this: " content ". Will String.trim() remove all spaces on these sides or just one space on each?
oneat
  • 10,778
  • 16
  • 52
  • 70
117
votes
11 answers

Java Replacing multiple different substring in a string at once (or in the most efficient way)

I need to replace many different sub-string in a string in the most efficient way. is there another way other then the brute force way of replacing each field using string.replace ?
Yossale
  • 14,165
  • 22
  • 82
  • 109
116
votes
9 answers

java: use StringBuilder to insert at the beginning

I could only do this with String, for example: String str = ""; for (int i = 0; i < 100; i++) { str = i + str; } Is there a way to achieve this with StringBuilder? Thanks.
user685275
  • 2,097
  • 8
  • 26
  • 32
116
votes
11 answers

How to force JS to do math instead of putting two strings together

I need javascript to add 5 to an integer variable, but instead it treats the variable as a string, so it write out the variable, then add 5 onto the end of the "string". How can I force it to do math instead? var dots =…
Sean
  • 8,401
  • 15
  • 40
  • 48