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
96
votes
6 answers

Using Locales with Java's toLowerCase() and toUpperCase()

I wanted code to convert all the characters in strings to uppercase or lowercase in Java. I found a method that goes something like this: public static String changelowertoupper() { String str = "CyBeRdRaGoN"; …
Arjun K P
  • 2,081
  • 4
  • 20
  • 33
95
votes
14 answers

How can I insert new line/carriage returns into an element.textContent?

Let's say I want to dynamically create a new DOM element and fill up its textContent/innerText with a JS string literal. The string is so long I would like to split it into three chunks: var h1 = document.createElement("h1"); h1.textContent = "This…
user1236489
  • 997
  • 1
  • 6
  • 5
95
votes
3 answers

Remove Sub String by using Python

I already extract some information from a forum. It is the raw string I have now: string = 'i think mabe 124 + but I don\'t have a big experience it just how I see it in my eyes
Wenhao.SHE
  • 1,903
  • 3
  • 17
  • 19
95
votes
6 answers

"\n" or '\n' or std::endl to std::cout?

It was many years now since I stopped using std::endl to end lines when writing to std::cout, and started using "\n" instead. But now I start seeing more snippets of code using '\n' instead, and I started wonder what might be best. Besides the…
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
95
votes
7 answers

What is the use of the c_str() function?

I understand c_str converts a string, that may or may not be null-terminated, to a null-terminated string. Is this true? Can you give some examples?
Amit Singh Tomar
  • 8,380
  • 27
  • 120
  • 199
95
votes
4 answers

Is there a native templating system for plain text files in Python?

I am looking for either technique or templating system for Python for formatting output to simple text. What I require is that it will be able to iterate through multiple lists or dicts. It would be nice if I would be able to define template into…
Gargauth
  • 2,495
  • 6
  • 27
  • 30
95
votes
7 answers

How are strings compared?

I'm wondering how Python does string comparison, more specifically how it determines the outcome when a less than < or greater than > operator is used. For instance if I put print('abc' < 'bac') I get True. I understand that it compares…
davelupt
  • 1,845
  • 4
  • 21
  • 32
95
votes
9 answers

How can I extract the file name and extension from a path in C++

I have a list of files stored in a .log in this syntax: c:\foto\foto2003\shadow.gif D:\etc\mom.jpg I want to extract the name and the extension from this files. Can you give a example of a simple way to do this?
Octavian
  • 4,519
  • 8
  • 28
  • 39
95
votes
9 answers

Titlecasing a string with exceptions

Is there a standard way in Python to titlecase a string (i.e. words start with uppercase characters, all remaining cased characters have lowercase) but leaving articles like and, in, and of lowercased?
yassin
  • 6,529
  • 7
  • 34
  • 39
95
votes
7 answers

Objective-C parse hex string to integer

I would like to know how to parse a hex string, representing a number, in Objective-C. I am willing to use both an objective, or a C-based method, either is fine. example: #01FFFFAB should parse into the integer: 33554347 Any help would be…
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
95
votes
0 answers

Javascript: How to remove characters from end of string?

I have a string "foo_bar" and "foo_foo_bar". How do I remove the last "_bar" from the string? So I'm left with "foo" and "foo_foo".
Albert
  • 3,611
  • 3
  • 28
  • 52
95
votes
11 answers

in python how do I convert a single digit number into a double digits string?

So say i have a = 5 i want to print it as a string '05'
Joe Schmoe
  • 1,815
  • 3
  • 15
  • 14
95
votes
7 answers

How slow is Python's string concatenation vs. str.join?

As a result of the comments in my answer on this thread, I wanted to know what the speed difference is between the += operator and ''.join() So what is the speed comparison between the two?
Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
95
votes
6 answers

How do I write a backslash (\) in a string?

I want to write something like this C:\Users\UserName\Documents\Tasks in a textbox: txtPath.Text = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)+"\Tasks"; I get the error: Unrecognized escape sequence. How do I write a…
user2509901
95
votes
4 answers

How to strip comma in Python string

How can I strip the comma from a Python string such as Foo, bar? I tried 'Foo, bar'.strip(','), but it didn't work.
msampaio
  • 3,394
  • 6
  • 33
  • 53
1 2 3
99
100