|  | Home · All Classes · Modules

# QStringRef Class Reference
[QtCore module]

The QStringRef class provides a thin wrapper around QString substrings. More...

### Methods

- __init__ (self)
- __init__ (self, QString, int, int)
- __init__ (self, QString)
- __init__ (self, QStringRef)
- QStringRef appendTo (self, QString)
- QChar at (self, int)
- clear (self)
- int compare (self, QString, Qt.CaseSensitivity cs = Qt.CaseSensitive)
- int compare (self, QStringRef, Qt.CaseSensitivity cs = Qt.CaseSensitive)
- int compare (self, QLatin1String, Qt.CaseSensitivity cs = Qt.CaseSensitive)
- QChar constData (self)
- int count (self)
- QChar data (self)
- bool isEmpty (self)
- bool isNull (self)
- int length (self)
- int localeAwareCompare (self, QString)
- int localeAwareCompare (self, QStringRef)
- int position (self)
- int size (self)
- QString string (self)
- QString toString (self)
- QChar unicode (self)

### Static Methods

- int compare (QStringRef, QString, Qt.CaseSensitivity cs = Qt.CaseSensitive)
- int compare (QStringRef, QStringRef, Qt.CaseSensitivity cs = Qt.CaseSensitive)
- int compare (QStringRef, QLatin1String, Qt.CaseSensitivity cs = Qt.CaseSensitive)
- int localeAwareCompare (QStringRef, QString)
- int localeAwareCompare (QStringRef, QStringRef)

### Special Methods

- bool __eq__ (self, QStringRef)
- bool __eq__ (self, QString)
- bool __eq__ (self, QLatin1String)
- bool __ge__ (self, QStringRef)
- bool __gt__ (self, QStringRef)
- bool __le__ (self, QStringRef)
- bool __lt__ (self, QStringRef)
- bool __ne__ (self, QStringRef)
- bool __ne__ (self, QString)
- bool __ne__ (self, QLatin1String)

## Detailed Description

The QStringRef class provides a thin wrapper around QString substrings.

QStringRef provides a read-only subset of the QString API.

A string reference explicitly references a portion of a string() with a given size(), starting at a specific position(). Calling toString() returns a copy of the data as a real QString instance.

This class is designed to improve the performance of substring handling when manipulating substrings obtained from existing QString instances. QStringRef avoids the memory allocation and reference counting overhead of a standard QString by simply referencing a part of the original string. This can prove to be advantageous in low level code, such as that used in a parser, at the expense of potentially more complex code.

For most users, there are no semantic benefits to using QStringRef instead of QString since QStringRef requires attention to be paid to memory management issues, potentially making code more complex to write and maintain.

Warning: A QStringRef is only valid as long as the referenced string exists. If the original string is deleted, the string reference points to an invalid memory location.

We suggest that you only use this class in stable code where profiling has clearly identified that performance improvements can be made by replacing standard string operations with the optimized substring handling provided by this class.

See also Implicitly Shared Classes.

## Method Documentation

### QStringRef.__init__ (self)

Constructs an empty string reference.

### QStringRef.__init__ (self, QString, int, int)

Constructs a string reference to the range of characters in the given string specified by the starting position and length in characters.

Warning: This function exists to improve performance as much as possible, and performs no bounds checking. For program correctness, position and length must describe a valid substring of string.

This means that the starting position must be positive or 0 and smaller than string's length, and length must be positive or 0 but smaller than the string's length minus the starting position; i.e, 0 <= position < string->length() and 0 <= length <= string->length() - position must both be satisfied.

### QStringRef.__init__ (self, QString)

Constructs a string reference to the given string.

### QStringRef.__init__ (self, QStringRef)

Constructs a copy of the other string reference.

### QStringRef QStringRef.appendTo (self, QString)

Appends the string reference to string, and returns a new reference to the combined string data.

### QChar QStringRef.at (self, int)

Returns the character at the given index position in the string reference.

The position must be a valid index position in the string (i.e., 0 <= position < size()).

### QStringRef.clear (self)

Clears the contents of the string reference by making it null and empty.

See also isEmpty() and isNull().

### int QStringRef.compare (self, QString, Qt.CaseSensitivity cs = Qt.CaseSensitive)

Compares the string s1 with the string s2 and returns an integer less than, equal to, or greater than zero if s1 is less than, equal to, or greater than s2.

If cs is Qt.CaseSensitive, the comparison is case sensitive; otherwise the comparison is case insensitive.

This function was introduced in Qt 4.5.

### int QStringRef.compare (self, QStringRef, Qt.CaseSensitivity cs = Qt.CaseSensitive)

This is an overloaded function.

Compares the string s1 with the string s2 and returns an integer less than, equal to, or greater than zero if s1 is less than, equal to, or greater than s2.

If cs is Qt.CaseSensitive, the comparison is case sensitive; otherwise the comparison is case insensitive.

This function was introduced in Qt 4.5.

### int QStringRef.compare (self, QLatin1String, Qt.CaseSensitivity cs = Qt.CaseSensitive)

This is an overloaded function.

Compares the string s1 with the string s2 and returns an integer less than, equal to, or greater than zero if s1 is less than, equal to, or greater than s2.

If cs is Qt.CaseSensitive, the comparison is case sensitive; otherwise the comparison is case insensitive.

This function was introduced in Qt 4.5.

### int QStringRef.compare (QStringRef, QString, Qt.CaseSensitivity cs = Qt.CaseSensitive)

This is an overloaded function.

Compares this string with the other string and returns an integer less than, equal to, or greater than zero if this string is less than, equal to, or greater than the other string.

If cs is Qt.CaseSensitive, the comparison is case sensitive; otherwise the comparison is case insensitive.

Equivalent to `compare(*this, other, cs)`.

This function was introduced in Qt 4.5.

See also QString.compare().

### int QStringRef.compare (QStringRef, QStringRef, Qt.CaseSensitivity cs = Qt.CaseSensitive)

This is an overloaded function.

Compares this string with the other string and returns an integer less than, equal to, or greater than zero if this string is less than, equal to, or greater than the other string.

If cs is Qt.CaseSensitive, the comparison is case sensitive; otherwise the comparison is case insensitive.

Equivalent to `compare(*this, other, cs)`.

This function was introduced in Qt 4.5.

See also QString.compare().

### int QStringRef.compare (QStringRef, QLatin1String, Qt.CaseSensitivity cs = Qt.CaseSensitive)

This is an overloaded function.

Compares this string with the other string and returns an integer less than, equal to, or greater than zero if this string is less than, equal to, or greater than the other string.

If cs is Qt.CaseSensitive, the comparison is case sensitive; otherwise the comparison is case insensitive.

Equivalent to `compare(*this, other, cs)`.

This function was introduced in Qt 4.5.

See also QString.compare().

### QChar QStringRef.constData (self)

Same as unicode().

### int QStringRef.count (self)

Returns the number of characters referred to by the string reference. Equivalent to size() and length().

See also position() and string().

### QChar QStringRef.data (self)

Same as unicode().

### bool QStringRef.isEmpty (self)

Returns true if the string reference has no characters; otherwise returns false.

A string reference is empty if its size is zero.

See also size().

### bool QStringRef.isNull (self)

Returns true if string() returns a null pointer or a pointer to a null string; otherwise returns true.

See also size().

### int QStringRef.length (self)

Returns the number of characters referred to by the string reference. Equivalent to size() and count().

See also position() and string().

### int QStringRef.localeAwareCompare (self, QString)

Compares s1 with s2 and returns an integer less than, equal to, or greater than zero if s1 is less than, equal to, or greater than s2.

The comparison is performed in a locale- and also platform-dependent manner. Use this function to present sorted lists of strings to the user.

On Mac OS X, this function compares according the "Order for sorted lists" setting in the International prefereces panel.

This function was introduced in Qt 4.5.

See also compare() and QTextCodec.locale().

### int QStringRef.localeAwareCompare (self, QStringRef)

This is an overloaded function.

Compares s1 with s2 and returns an integer less than, equal to, or greater than zero if s1 is less than, equal to, or greater than s2.

The comparison is performed in a locale- and also platform-dependent manner. Use this function to present sorted lists of strings to the user.

This function was introduced in Qt 4.5.

### int QStringRef.localeAwareCompare (QStringRef, QString)

This is an overloaded function.

Compares this string with the other string and returns an integer less than, equal to, or greater than zero if this string is less than, equal to, or greater than the other string.

The comparison is performed in a locale- and also platform-dependent manner. Use this function to present sorted lists of strings to the user.

This function was introduced in Qt 4.5.

### int QStringRef.localeAwareCompare (QStringRef, QStringRef)

This is an overloaded function.

Compares this string with the other string and returns an integer less than, equal to, or greater than zero if this string is less than, equal to, or greater than the other string.

The comparison is performed in a locale- and also platform-dependent manner. Use this function to present sorted lists of strings to the user.

This function was introduced in Qt 4.5.

### int QStringRef.position (self)

Returns the starting position in the referenced string that is referred to by the string reference.

See also size() and string().

### int QStringRef.size (self)

Returns the number of characters referred to by the string reference. Equivalent to length() and count().

See also position() and string().

### QString QStringRef.string (self)

Returns a pointer to the string referred to by the string reference, or 0 if it does not reference a string.

See also unicode().

### QString QStringRef.toString (self)

Returns a copy of the string reference as a QString object.

If the string reference is not a complete reference of the string (meaning that position() is 0 and size() equals string()->size()), this function will allocate a new string to return.

See also string().

### QChar QStringRef.unicode (self)

Returns a Unicode representation of the string reference. Since the data stems directly from the referenced string, it is not null-terminated unless the string reference includes the string's null terminator.

See also string().

### bool QStringRef.__eq__ (self, QStringRef)

### bool QStringRef.__eq__ (self, QString)

### bool QStringRef.__eq__ (self, QLatin1String)

### bool QStringRef.__ge__ (self, QStringRef)

### bool QStringRef.__gt__ (self, QStringRef)

### bool QStringRef.__le__ (self, QStringRef)

### bool QStringRef.__lt__ (self, QStringRef)

### bool QStringRef.__ne__ (self, QStringRef)

### bool QStringRef.__ne__ (self, QString)

### bool QStringRef.__ne__ (self, QLatin1String)