Contents
String
In computer programming, a string is a type of data that may contain letters, numbers, and symbols, and that is stored as a sequence of text.
In most programming languages, pieces of data are recognized as strings when surrounded by quotation marks. Strings are stored as text, even if they contain numbers. For example, in JavaScript, if a variable is assigned the value 2, that value would be stored as an integer, and could be used to perform calculations. However, if a variable is assigned the value “2”, that value would be stored as a text string, and could not be used to perform calculations.
Strings are stored in a specific sequence. For example, the string “Mary had a little lamb” is stored exactly as it is typed, and a search for the string “lamb little Mary” would not find the stored string unless an advanced algorithm, such as those used by search engines, was used to find partial matches.
Websites use strings to store things like usernames, encrypted passwords, and the text content of a blog post. Virtually all of the text you read on the web is stored as a string in a database server somewhere.
While strings can be used to hold many types of symbols, letter, and numbers, in many cases the content that can be used to fill a specific string may be limited. For example, if you use WordPress to administer a website, the status of every post is stored as a string, with one of eight possible values assigned to the string: “publish”, “pending”, “draft”, “private”, “trash”, “auto-draft”, “inherit”, and “future”. Since the software itself assigns the value to this string, it isn’t possible to change the value of this string to anything other than one of the predefined values unless you do so manually by accessing the database, in which case the post will break and not be rendered on the site.
Also See: Script, JavaScript
Frequently Asked Questions
What letters, symbols, and numbers can a string include?
In most programming languages strings are the wild card data type that can be used to hold information and values that don’t fit inside of any other data types. This means that virtually any letter, number, or symbol can be stored in a string. However, when using strings to store information, think about how that information will be used. For example, if you’re storing data that will later be processed by a browser, think about the way the browser will interpret symbols like “&” and “<” which may not render correctly when rendered by the browser’s HTML engine.
What is an empty string?
It is often useful to have a string that is equivalent to an empty text field. In those cases, an empty string is stored and compared against the text field to determine if it is empty. For example, if a website is using JavaScript to determine that every required field in a web form has been completed they will use an empty string and compare it to every required field. If one of the required fields is found to be equal to the empty string then it will be apparent that the required field has been left blank.
How are strings used to locate information?
Search engines compare search terms to the strings of information stored in their index. Typically, they will break the search terms into several substrings and compare each substring to the strings of information stored in their index, checking for both partial and complete matches. Complex algorithms are then used to determine which matches are the most relevant.