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
102
votes
10 answers

How to compare Unicode characters that "look alike"?

I fall into a surprising issue. I loaded a text file in my application and I have some logic which compares the value having µ. And I realized that even if the texts are same the compare value is false. Console.WriteLine("μ".Equals("µ")); //…
D J
  • 6,908
  • 13
  • 43
  • 75
102
votes
10 answers

How to pad a string to a fixed length with spaces

I'm sure this is covered in plenty of places, but I don't know the exact name of the action I'm trying to do so I can't really look it up. I've been reading an official Python book for 30 minutes trying to find out how to do this. Problem: I need…
user2977230
  • 8,897
  • 5
  • 14
  • 9
102
votes
5 answers

How can I check the first character in a string in Bash or Unix shell?

I'm writing a script in Unix where I have to check whether the first character in a string is "/" and if it is, branch. For example, I have a string: /some/directory/file I want this to return 1, and: server@10.200.200.20:/some/directory/file to…
canecse
  • 1,772
  • 3
  • 16
  • 20
102
votes
9 answers

How can I insert a character after every n characters in javascript?

I have a string: "The quick brown fox jumps over the lazy dogs." I want to use JavaScript (possibly with jQuery) to insert a character every n characters. For example I want to call: var s = "The quick brown fox jumps over the lazy dogs."; var…
brendan
  • 29,308
  • 20
  • 68
  • 109
102
votes
1 answer

Set precision of std::to_string when converting floating point values

In C++11, std::to_string defaults to 6 decimal places when given an input value of type float or double. What is the recommended, or most elegant, method for changing this precision?
learnvst
  • 15,455
  • 16
  • 74
  • 121
102
votes
19 answers

Extract digits from string - StringUtils Java

I have a String and I want to extract the (only) sequence of digits in the string. Example: helloThisIsA1234Sample. I want the 1234 It's a given that the sequence of digits will occur only once within the string but not in the same position. (for…
NinaNa
  • 1,575
  • 6
  • 15
  • 21
102
votes
5 answers

Unrecognized escape sequence for path string containing backslashes

The following code generates a compiler error about an "unrecognized escape sequence" for each backslash: string foo = "D:\Projects\Some\Kind\Of\Pathproblem\wuhoo.xml"; I guess I need to escape backslash? How do I do that?
Kjensen
  • 12,447
  • 36
  • 109
  • 171
101
votes
11 answers

C++/CLI Converting from System::String^ to std::string

Can someone please post a simple code that would convert, System::String^ To, C++ std::string I.e., I just want to assign the value of, String^ originalString; To, std::string newString;
sivabudh
  • 31,807
  • 63
  • 162
  • 228
101
votes
8 answers

Why is splitting a string slower in C++ than Python?

I'm trying to convert some code from Python to C++ in an effort to gain a little bit of speed and sharpen my rusty C++ skills. Yesterday I was shocked when a naive implementation of reading lines from stdin was much faster in Python than C++ (see…
JJC
  • 9,547
  • 8
  • 48
  • 53
101
votes
10 answers

What is the best way to chop a string into chunks of a given length in Ruby?

I have been looking for an elegant and efficient way to chunk a string into substrings of a given length in Ruby. So far, the best I could come up with is this: def chunk(string, size) …
MiniQuark
  • 46,633
  • 36
  • 147
  • 183
101
votes
12 answers

How can I truncate my strings with a "..." if they are too long?

Hope somebody has a good idea. I have strings like this: abcdefg abcde abc What I need is for them to be trucated to show like this if more than a specified lenght: abc .. abc .. abc Is there any simple C# code I can use for this?
Melony
  • 1,341
  • 3
  • 10
  • 6
101
votes
11 answers

How to convert string to IP address and vice versa

how can I convert a string ipAddress (struct in_addr) and vice versa? and how do I turn in unsigned long ipAddress? thanks
Safari
  • 11,437
  • 24
  • 91
  • 191
101
votes
11 answers

I want to truncate a text or line with ellipsis using JavaScript

I'm looking for a simple script which can truncate a string with ellipsis (...) I want to truncate something like 'this is a very long string' to 'this is a ve...' I don't want to use CSS or PHP.
Dollar Friend
  • 1,019
  • 2
  • 7
  • 4
101
votes
9 answers

Insert some string into given string at given index

How can I insert some text into an existing string? For example, suppose I have a string "Name Age Group Class Profession". How can I insert the third word three more times before the fourth, to get "Name Age Group Group Group Group Class…
james
  • 1,087
  • 2
  • 10
  • 13
101
votes
10 answers

String Padding in C

I wrote this function that's supposed to do StringPadRight("Hello", 10, "0") -> "Hello00000". char *StringPadRight(char *string, int padded_len, char *pad) { int len = (int) strlen(string); if (len >= padded_len) { return string; …
jackson