What is “String”?

In computer parlance, a String is defined as a data type that holds multiple character or integer values in one particular sequence. For e.g., “I ate 2 pizzas” is a string comprising of different characters, while “Hello World!” is another which also includes a special character. String is accepted as a data type in any programming language, generally comprising of character data types and can either have a fixed length or variable length depending on the programming language and its use.

The length of the string is measured by counting the individual characters that comprise the string, including spaces, special characters and integers. In all programming languages, a string is denoted with either single or double quotes, before and after the actual content. The object inside the quotes is called a string and can contain multiple values including characters, integers or spaces in any sequence. A Null character is generally added to the end of a string to indicate its completion like in the programming language C. This null character is also used for counting the length of a string.

“Example of a string object showing how it is stored in memory”

String in technical terms is an array of characters in a sequence, and its length can either be variable or fixed. An array is a special data type and also a method of storing data types like characters or integers in continuous sequence. Since a string is also comprised of data elements stored in a sequence, an array is the best way to store strings. A string along with its functions is used best when communicating data or information to the user of a program from the program itself.

How are Strings Represented?

There are two factors that determine how a string or a string data type is represented.

  1. The character set that is defined (like the alphabet).
  2. The character encoding that is used.

In previous years, ASCII (based on U.S English) was the standard for the character encoding used in string reference, but lately UNICODE has taken over the role of implementing strings, because it attempts to provide codes for all written languages, even extinct ones. Hence, in today’s non-ASCII world, a string is a sequence made up of one or more UTF-8 (a variable width character encoding derived from UNICODE standard) characters, where data types can be a character, an integer or digit, white space or any other symbol.

Length of a String

A string can either have a finite length or variable length depending on the need of the situation. A finite length string means that the amount of memory allocated would be fixed or static and cannot be changed, unlike a variable length string, memory for which is allocated dynamically as per input or calculated during run time as per requirement. In most programming languages, string object cannot be changed once memory has been allocated, or rather they are immutable once a value has been assigned. C++ is one language where the string objects are mutable or can be changed after they have been created or assigned a value.

String Functions

There are a variety of string functions that are regularly used in programming languages like Java, C++, Python, etc. to manipulate a string or get information about it or sometimes do both. These functions enable operations to be performed on a string, as and when required, while programming. Some of the popular functions include concatenate, length, substring, compare, contains among others.

  • Concatenate

This is the process of joining or combining two string elements into one, generally using an operator. This is a very frequently used function while working with strings. One example of this could be to concatenate the first and last name of a customer to display their full name. There might be slight variations in the syntax of the function depending on the programming language used and element involved. Concatenation can also be called as addition of strings. The sequence of characters in the string element still cannot be altered though using this function.

“Example of two strings being concatenated or joined together to make one string”
  • Substring

As the name suggests, the function substring involves returning a contiguous sequence of a particular string. For e.g., “I ate” is a substring of “I ate 2 pizzas”. The substring can either be a prefix or a suffix if one considers the whole element of the string.

  • Length

The length function is used to return the length of a particular string. It is generally used in addition to some of the other functions of string programming like substring or concatenate with the purpose of manipulating data to a specific end.

  • Compare

Another important function that is used in relation with string is compare. This is used to compare the contents of two different strings and return a particular result depending on whether the compared strings are same or different. An example of this could be to check the password while logging into any account.

Note: Various programming languages may have differing syntax for the different functions of string.

  • Reverse

This function is used to reverse the content of a string variable. In C language the function is named as strrev( ). In python reverse of a string is very easy using the slicing and indexing.

Strings in Programming Languages

Over the years, there have been several programming languages that have been specifically designed to facilitate the development of application programs for processing of string objects. Some of them are Perl, sed and Tcl. Perl takes an extremely flexible approach by allowing its strings data type to contain any kind of data, even binary.

Strings in C

In C programming language, a language widely used to develop operating systems, there is a very different approach to strings. There is no special data type for strings; instead, strings are treated as an array of char data type. But the C language does allow a lot of standard string operations to be performed like the functions described earlier.

Here are some points to note regarding the use of string elements or objects in C:

  • They are represented as arrays of character data type.
  • We can constitute a string in C programming by assigning in sequence one character after another into an array of characters.
  • We can also constitute a string in C programming by assigning a complete string enclosed in double quotes.
  • We can print a string in sequence by using an array subscript or a complete string by using an array name without subscript.
  • The last character of every string is always a null character, i.e., ‘’, thus denoting the end of the string.

Strings in Python

In Python programming language, creating strings is as simple as assigning a string into a variable using single or double quotes. One unique feature of Python is the absence of a character data type, instead, these are treated as strings of length one, and also be considered a substring.

Strings in Java

In Java, a popular programming language, strings are a data type by itself, which means that you can define string objects directly instead of using an array of characters. A string object can be created in Java either by string literal or by a new keyword.

Common Mistakes

  • Since there are various programming languages and all of them use strings, there can be confusion regarding operations or functions using strings and the syntax to be followed as they can be different for each language.
  • Depending on the language, a string can also be mutable or immutable, and hence should be taken note of while doing string manipulation.
  • Do remember to double check the data type of the string before using in any program.

Context and Applications  

This topic is significant in the professional exams for both undergraduate and graduate courses, especially for 

  • Bachelor in Computer Science
  • Masters in Computer Science
  • Bachelor of Computer Application
  • Masters of Computer Application
  • Bachelor of technology in Computer Science and engineering
  • Masters of technology in Computer Science and engineering
  • Data Types
  • String Manipulation
  • String Functions
  • Arrays

Want more help with your computer science homework?

We've got you covered with step-by-step solutions to millions of textbook problems, subject matter experts on standby 24/7 when you're stumped, and more.
Check out a sample computer science Q&A solution here!

*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.

Search. Solve. Succeed!

Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.

Tagged in
EngineeringComputer Science

Programming

String

Immutable property of String Class

String Homework Questions from Fellow Students

Browse our recently answered String homework questions.

Search. Solve. Succeed!

Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.

Tagged in
EngineeringComputer Science

Programming

String

Immutable property of String Class