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

What is the difference between []string and ...string in golang?

In the Go language, []string is a string array and we also use ...string as a parameter. What is the difference? Function definition: func f(args ...string) {} Can I call this function like below? args := []string{"a", "b"} f(args)
user1746360
  • 1,164
  • 2
  • 8
  • 12
98
votes
4 answers

Looping through python regex matches

I want to turn a string that looks like this: ABC12DEF3G56HIJ7 into 12 * ABC 3 * DEF 56 * G 7 * HIJ I want to construct the correct set of loops using regex matching. The crux of the issue is that the code has to be completely general because I…
da5id
  • 1,039
  • 1
  • 9
  • 6
98
votes
9 answers

Is it possible to modify a string of char in C?

I have been struggling for a few hours with all sorts of C tutorials and books related to pointers but what I really want to know is if it's possible to change a char pointer once it's been created. This is what I have tried: char *a = "This is a…
Matthew Stopa
  • 3,793
  • 6
  • 42
  • 51
97
votes
2 answers

Changing a column type to longer strings in rails

At the first migration, I declared on a column content to be string Activerecord made it to be string(255) according to annotate gem. After I push the app to heroku, which uses postgres, if I enter in the form in content a string longer than 255 I…
97
votes
4 answers

Intellij IDEA plus sign when String wrap

I'm using Intellij IDEA 10.5 Community. If I have a long String and want to split in in mulitple lines I press ENTER key in the middle of a String and get this: String str = "ONE LONG" + "STRING"; Is it possible to put the + sign in…
kovica
  • 2,443
  • 3
  • 21
  • 25
97
votes
4 answers

Strip last two characters of a column in MySQL

I have an SQL column where the entries are strings. I need to display those entries after trimming the last two characters, e.g. if the entry is 199902345 it should output 1999023. I tried looking into TRIM but looks like it offers to trim only if…
Lucky Murari
  • 12,672
  • 5
  • 22
  • 43
97
votes
26 answers

How to get file extension from string in C++

Given a string "filename.conf", how to I verify the extension part? I need a cross platform solution.
JeffV
  • 52,985
  • 32
  • 103
  • 124
97
votes
5 answers

How to extend C# built-in types, like String?

I need to Trim a String. But I want to remove all the repeated blank spaces within the String itself, not only at the end or at the start of it. I could do it with a method like: public static string ConvertWhitespacesToSingleSpaces(string value) { …
Girardi
  • 2,734
  • 3
  • 35
  • 50
97
votes
2 answers

How can I split up a long f-string in Python?

I am getting a line too long PEP 8 E501 issue. f'Leave Request created successfully. Approvers sent the request for approval: {leave_approver_list}' I tried using a multi-line string, but that brings in a \n, which breaks my test: f'''Leave Request…
tread
  • 10,133
  • 17
  • 95
  • 170
97
votes
5 answers

c++ parse int from string

Possible Duplicate: How to parse a string to an int in C++? I have done some research and some people say to use atio and others say it's bad, and I can't get it to work anyways. So I just want to ask flat out, whats the right way to convert a…
kralco626
  • 8,456
  • 38
  • 112
  • 169
97
votes
2 answers

Format string, integer with leading zeros

I want to convert an integer value to a string with leading zeroes (if necessary) such that the string has 3 total characters. For example, 5 would become "005", and 10 would become "010". I've tried this code: NSString* strName = [NSString…
ghiboz
  • 7,863
  • 21
  • 85
  • 131
97
votes
18 answers

Convert True/False value read from file to boolean

I'm reading a True - False value from a file and I need to convert it to boolean. Currently it always converts it to True even if the value is set to False. Here's a MWE of what I'm trying to do: with open('file.dat', mode="r") as f: for line in…
Gabriel
  • 40,504
  • 73
  • 230
  • 404
97
votes
19 answers

Check if a String contains a special character

How do you check if a String contains a special character like: [,],{,},{,),*,|,:,>,
blue
  • 971
  • 1
  • 7
  • 3
97
votes
4 answers

Remove/Add Line Breaks after Specific String using Sublime Text

Using Sublime Text 2 - Is it possible to insert a line break/text return after a specific String in a text file e.g. by using the Find ‣ Replace tool? (Bonus question: Is it possible to remove all line breaks after a specific String)
Bernd
  • 11,133
  • 11
  • 65
  • 98
97
votes
3 answers

Split and join C# string

Possible Duplicate: First split then join a subset of a string I'm trying to split a string into an array, take first element out (use it) and then join the rest of the array into a seperate string. Example: theString = "Some Very Large String…
Gaui
  • 8,723
  • 16
  • 64
  • 91