(Computer Science): A series of ordered characters. Letters and numerals are often encoded into binary by ASCII or EBCDIC.

"String" is a pre-defined standard class in Java which has a lot of member functions for doing things with strings.

String s1 = "abc";

The methods like replace(), tolowerCase() etc. are accessed by using s1.replace(), s1.tolowerCase() etc. When you call each of these methods you have to pass parameters what parameters the method requires you to pass, like for example if the method requires you to pass an int you have to pass an int even if it's big and it makes your weewee hurt you still have to pass it. An int is an integer and they're very pointy and they hurt.

For e.g. s1.charAt(2) will return the third character in the string s1. (note the index of string starts from 0, i.e. first character has index 0, and so on, starting at zero which is before one and it's a very small number that it's so small you can't see it but it's there at the beginning of every string that you make in the Java language that you program with when you're programming in Java which has strings.)

A string is a common data type only somewhat less ubiquitious than an integer type. Most computer languages have at least some degree of built-in support for a type that is called or is equivelent to a string.

There are variations as to what is considered to constitute a string, which will be discussed below. However, all string types have this much in common: they are a list of ordered characters of some known length. A string is the most common way to store textual information of any kind; this includes a textual representation of numbers. However, the content of a string does not need to be textual or even human-readable.

Most languages allow in-situ representations of strings, usually by enclosing the appropriate text in either single or double quotes.

Beyond creation and destruction, here are some of the most common operations that are performed on data of type string:

One very important variation in the implementation of strings is how the length of the string is determined. Ideally, the length of a string should be able to be determined quickly and expanded as necessary.

  • One school of thought on this subject of length determination is null-terminated strings: that is, the last actual character of the string is followed by a character having the value zero. This style of strings is also known as ASCIIZ (or ASCIZ) strings, C-style strings, or just C strings. Traditionally, C strings are zero-indexed; meaning, that when referring to a particular character within the string, the first character is numbered 0, the second 1, and so forth.

    Advantages of this implementation is that there is no explicit limitation on the length of the string; it is fairly easy to implement; and this style of string is almost universally understood. Disadvantages include: calculation of the length of the string is relatively slow; the content of the strings is restricted in that they connot contain a null character; and bugs regarding buffer overrun are difficult to avoid and debug.

  • The primary alternative to null-terminated strings are indexed strings, more commonly known as Pascal strings (named not for the mathemeticican and philosopher, but the language that popularized them). These strings are prefixed by some encoding of the total length of the string; thus, the length can be determined without actually counting each character. Pascal strings may or may not be zero-indexed. The length count makes resizing and manipulation easier and safer, but unfortantely many languages select a naive implementation and allocate a fixed amount of space for each string, cutting back on dynamic reallocating at the cost of unnecessary consumption. The other big problem with Pascal strings is that their size is limited to the upper limit on the type storing their length. Turbo Pascal uses a single length byte: therefore, all strings in Turbo Pascal are limited to 255 characters. Java, on the other hand, uses a 32-bit integer for storing strings in memory, making string size effectively limited only by available memory; unfortunately, this advantages is negated by a ridiculous serialization scheme that causes strings greater than 64K characters to cause exceptions when written to any stream.

Another varation within the sphere of strings is the definition of a character. Platforms using the ASCII character set usually employ characters of one byte. Increasingly, modern languages have support for wide characters: that is, characters of two bytes. C++, for example, has a wchar_t type. The character set used within a wide-character type is dependant on the underlying platform, but a common one is Unicode. Many languages allow manipulation of both single- and double-byte character strings, in which case one must take care to convert between them when necessary. The Win32 API, for example, defines types LPSTR and LPWSTR as ASCII and Unicode strings respectively.


C strings are usually described in that languages a char *: that is, a pointer to a character. This fact may be obscured by typedefs, but this is typically the underlying implementation. C provides a library of string-manipulation functions in the header string.h; some of these functions are listed along with the string operations above. Most of these functions are distinguished by the prefix "str".

C++ has a template class called string that implements strings as a managed array, although one could easily instantiate a class of string based around any kind of character type, due to the magic of the STL and templates.

Pascal supports a built-in type called STRING, which, depending on the implementation, may or may not be a keyword. Also, the STRING type may or may not accept a maximum length as an argument, using square brackets or parentheses, again, depending on implementation. There are several standard Pascal functions for manipulating strings, although, unlike with C, determining which standard functions deal with strings is not always easy; for example, the Copy function will extract a substring, while the Move function has nothing to do with strings whatsoever.

Java has a class java.lang.String that receives special syntactic sugar from the compiler, mostly in the form of the operator + for concatenation. Double-quoted text in the language automatically instantiates an instance of this class.


AT has corrected facts in this node.

The below is a C++ implementation of a very basic string class, meant to illustrate the concept. It encapsulates much of the common used functionality of the str functions, but is by no means complete.

Example usage:

MyString str1 = "Foobar";
MyString str2 = str.Slice(1,3);
// str2 now contains "Foo"
MyString str3 = str2;
// str3 now equals str 2.
MyString str4 = str2 + str1;
// str4 now contains "FooFoobar"
MyString str5 = "FooFoobar";
if ( str5 == str4 ) cout << "str5 == str4";
cout << str5[1]; // outputs "o"


class MyString {
  public:
    char *data;
    MyString();
    MyString(MyString& rhs);
    MyString(const char* rhs);
    ~MyString();
    MyString& operator+(const MyString& rhs);
    MyString& operator=(const MyString& rhs);
    MyString& operator=(const char* rhs);
    char operator[](int Index);
    char *Slice(int from, int offset);
    operator char *() const { return data; }
    bool operator==(const MyString& rhs);
    char* CString();
    size_t Strlen();
};

MyString::MyString() {
  data = 0;
}

MyString::MyString(MyString& rhs ) {
  data = new char[rhs.Strlen()];
  strcpy(data,rhs.data);
}

MyString::MyString(const char* rhs) {
  if (! rhs ) {
    data = 0;
    return;
  }
  data = new char[strlen(rhs)];
  strcpy(data,rhs);
}

MyString::~MyString() {
  delete[] data;
}

MyString& MyString::operator+( const MyString& rhs ) {
  MyString* NewString = new MyString;
  NewString->data = new char[strlen(rhs.data)+strlen(data)];
  strcpy(NewString->data,data);
  strcat(NewString->data,rhs.data);
  return *NewString;
}

MyString& MyString::operator=( const MyString& rhs ) {
  if ( rhs == *this ) // assignment to self is bad!
    return *this;

  strcpy(data,rhs.data);
  return *this;
}

bool MyString::operator==( const MyString& rhs ) {
  if ( strcmp(rhs.data, data) == 0 )
    return true;
  return false;
}

MyString& MyString::operator=(const char* rhs) {
  if ( rhs == data )
    return *this;
  strcpy(data,rhs);
  return *this;
}

char MyString::operator[](int index) {
  if ( index > Strlen() || index < 0 )
    return 0;
  return data[index];
}

char *MyString::Slice(int from, int offset) {
  size_t size = Strlen();
  size_t SizeOfSlice = offset;
  if ( from + offset > size || from + offset < 0  )
    return 0;
  char *New = new char[SizeOfSlice];
  char *DataIter = data + from;
  for (int i = 0; i < SizeOfSlice; i++ ) {
    *New++ = *DataIter++;
  }
  *New = '\0';
  return (New-SizeOfSlice);
}

size_t MyString::Strlen() {
  return strlen(data);
}

char *MyString::CString() {
  return data;
}

String (string), n. [OE. string, streng, AS. streng; akin to D. streng, G. strang, Icel. strengr, Sw. sträng, Dan. stræng; probably from the adj., E. strong (see Strong); or perhaps originally meaning, twisted, and akin to E. strangle.]

1.

A small cord, a line, a twine, or a slender strip of leather, or other substance, used for binding together, fastening, or tying things; a cord, larger than a thread and smaller than a rope; as, a shoe string; a bonnet string; a silken string. Shak.

Round Ormond's knee thou tiest the mystic string.
Prior.

2.

A thread or cord on which a number of objects or parts are strung or arranged in close and orderly succession; hence, a line or series of things arranged on a thread, or as if so arranged; a succession; a concatenation; a chain; as, a string of shells or beads; a string of dried apples; a string of houses; a string of arguments. "A string of islands." Gibbon.

3.

A strip, as of leather, by which the covers of a book are held together. Milton.

4.

The cord of a musical instrument, as of a piano, harp, or violin; specifically (pl.), the stringed instruments of an orchestra, in distinction from the wind instruments; as, the strings took up the theme. "An instrument of ten strings." Ps. xxx. iii. 2.

Me softer airs befit, and softer strings
Of lute, or viol still.
Milton.

5.

The line or cord of a bow. Ps. xi. 2.

He twangs the grieving string.
Pope.

6.

A fiber, as of a plant; a little, fibrous root.

Duckweed putteth forth a little string into the water, from the bottom.
Bacon.

7.

A nerve or tendon of an animal body.

The string of his tongue was loosed.
Mark vii. 35.

8. (Shipbuilding)

An inside range of ceiling planks, corresponding to the sheer strake on the outside and bolted to it.

9. (Bot.)

The tough fibrous substance that unites the valves of the pericap of leguminous plants, and which is readily pulled off; as, the strings of beans.

10. (Mining)

A small, filamentous ramification of a metallic vein. Ure.

11. (Arch.)

Same as Stringcourse.

12. (Billiards)

The points made in a game.

String band (Mus.), a band of musicians using only, or chiefly, stringed instruments. --
String beans.
(a) A dish prepared from the unripe pods of several kinds of beans; -- so called because the strings are stripped off.
(b) Any kind of beans in which the pods are used for cooking before the seeds are ripe; usually, the low bush bean. --
To have two strings to one's bow, to have a means or expedient in reserve in case the one employed fails.

 

© Webster 1913


String (string), v. t. [imp. Strung (strung); p. p. Strung (R. Stringed (stringd)); p. pr. & vb. n. Stringing.]

1.

To furnish with strings; as, to string a violin.

Has not wise nature strung the legs and feet
With firmest nerves, designed to walk the street?
Gay.

2.

To put in tune the strings of, as a stringed instrument, in order to play upon it.

For here the Muse so oft her harp has strung,
That not a mountain rears its head unsung.
Addison.

3.

To put on a string; to file; as, to string beads.

4.

To make tense; to strengthen.

Toil strung the nerves, and purified the blood.
Dryden.

5.

To deprive of strings; to strip the strings from; as, to string beans. See String, n., 9.

 

© Webster 1913


String (?), n.

1.

(a)

In various indoor games, a score or tally, sometimes, as in American billiard games, marked by buttons threaded on a string or wire.

(b)

In various games, competitions, etc., a certain number of turns at play, of rounds, etc.

2. (Billiards & Pool)

(a)

The line from behind and over which the cue ball must be played after being out of play as by being pocketed or knocked off the table; -- called also string line.

(b)

Act of stringing for break.

3.

A hoax; a trumped-up or "fake" story. [Slang]

 

© Webster 1913


String, v. t.

To hoax; josh; jolly. [Slang]

 

© Webster 1913


String, v. i.

To form into a string or strings, as a substance which is stretched, or people who are moving along, etc.

 

© Webster 1913

Log in or register to write something here or to contact authors.