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
112
votes
7 answers

How is null + true a string?

Since true is not a string type, how is null + true a string ? string s = true; //Cannot implicitly convert type 'bool' to 'string' bool b = null + true; //Cannot implicitly convert type 'string' to 'bool' What is the reason behind this?
Javed Akram
  • 15,024
  • 26
  • 81
  • 118
112
votes
12 answers

Is StringUtils.EMPTY recommended?

Do you use StringUtils.EMPTY instead of ""? I mean either as a return value or if you set a the value of a String variable. I don't mean for comparison, because there we use StringUtils.isEmpty()
keuleJ
  • 3,418
  • 4
  • 30
  • 51
112
votes
21 answers

How to find nth occurrence of character in a string?

Similar to a question posted here, am looking for a solution in Java. That is, how to find the index of nth occurrence of a character/string from a string? Example: "/folder1/folder2/folder3/". In this case, if I ask for 3rd occurrence of slash…
Gnanam
  • 10,613
  • 19
  • 54
  • 72
112
votes
15 answers

Is there a way of making strings file-path safe in c#?

My program will take arbitrary strings from the internet and use them for file names. Is there a simple way to remove the bad characters from these strings or do I need to write a custom function for this?
Martin Doms
  • 8,598
  • 11
  • 43
  • 60
112
votes
6 answers

What linux shell command returns a part of a string?

I want to find a linux command that can return a part of the string. In most programming languages, it's the substr() function. Does bash have any command that can be used for this purpose. I want to be able to do something like this... substr…
Binny V A
  • 2,036
  • 3
  • 20
  • 23
112
votes
16 answers

Java equivalents of C# String.Format() and String.Join()

I know this is a bit of a newbie question, but are there equivalents to C#'s string operations in Java? Specifically, I'm talking about String.Format and String.Join.
Omar Kooheji
  • 54,530
  • 68
  • 182
  • 238
112
votes
4 answers

Python str vs unicode types

Working with Python 2.7, I'm wondering what real advantage there is in using the type unicode instead of str, as both of them seem to be able to hold Unicode strings. Is there any special reason apart from being able to set Unicode codes in unicode…
Caumons
  • 9,341
  • 14
  • 68
  • 82
112
votes
3 answers

Regular expression - starting and ending with a character string

I would like to write a regular expression that starts with the string "wp" and ends with the string "php" to locate a file in a directory. How do I do it? Example file: wp-comments-post.php
Ken Shoufer
  • 1,393
  • 4
  • 10
  • 14
111
votes
11 answers

Why cannot cast Integer to String in java?

I found some strange exception: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String How it can be possible? Each object can be casted to String, doesn't it? The code is: String myString = (String)…
user710818
  • 23,228
  • 58
  • 149
  • 207
111
votes
2 answers

Check if string is upper, lower, or mixed case in Python

I want to classify a list of string in Python depending on whether they are upper case, lower case, or mixed case How can I do this?
shreyas
  • 2,510
  • 4
  • 19
  • 20
111
votes
1 answer

Why is f'{{{74}}}' the same as f'{{74}}' with f-Strings?

f-Strings are available from Python 3.6 and are very useful for formatting strings: >>> n='you' >>> f'hello {n}, how are you?' 'hello you, how are you?' Reading more about them in Python 3's f-Strings: An Improved String Formatting Syntax (Guide).…
fedorqui
  • 275,237
  • 103
  • 548
  • 598
111
votes
7 answers

JavaScript: How many times a character occurs in a string?

Is there a simple way to check how many times a character appears in a String?
Ruth
  • 5,646
  • 12
  • 38
  • 45
111
votes
0 answers

How to compare 'μ' and 'µ' in C#

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
111
votes
16 answers

Java - Create a new String instance with specified length and filled with specific character. Best solution?

I did check the other questions; this question has its focus on solving this particular question the most efficient way. Sometimes you want to create a new string with a specified length, and with a default character filling the entire string. ie,…
Stefan Hendriks
  • 4,705
  • 5
  • 34
  • 43
111
votes
6 answers

Splitting String with delimiter

I am currently trying to split a string 1128-2 so that I can have two separate values. For example, value1: 1128 and value2: 2, so that I can then use each value separately. I have tried split() but with no success. Is there a specific way Grails…
thehoule64
  • 1,761
  • 5
  • 15
  • 22