Loading...
Searching...
No Matches
juce::StringRef Class Referencefinal

## Detailed Description

A simple class for holding temporary references to a string literal or String.

Unlike a real String object, the StringRef does not allocate any memory or take ownership of the strings you give to it - it simply holds a reference to a string that has been allocated elsewhere. The main purpose of the class is to be used instead of a const String& as the type of function arguments where the caller may pass either a string literal or a String object. This means that when the called uses a string literal, there's no need for an temporary String object to be allocated, and this cuts down overheads substantially.

Because the class is simply a wrapper around a pointer, you should always pass it by value, not by reference.

void myStringFunction1 (const String&);
void myStringFunction2 (StringRef);
myStringFunction1 ("abc"); // Implicitly allocates a temporary String object.
myStringFunction2 ("abc"); // Much faster, as no local allocations are needed.
juce::StringRef::StringRef
StringRef(const char *stringLiteral) noexcept
Creates a StringRef from a raw string literal.
juce::String
The JUCE String class!
Definition juce_String.h:67

For examples of it in use, see the XmlElement or StringArray classes.

Bear in mind that there are still many cases where it's better to use an argument which is a const String&. For example if the function stores the string or needs to internally create a String from the argument, then it's better for the original argument to already be a String.

See also
  String

The documentation for this class was generated from the following file:

- juce_core/text/juce_StringRef.h

## Public Member Functions

| StringRef (const char *stringLiteral) noexcept
| Creates a StringRef from a raw string literal.
| StringRef (String::CharPointerType stringLiteral) noexcept
| Creates a StringRef from a raw char pointer.
| StringRef (const String &string) noexcept
| Creates a StringRef from a String.
| StringRef (const std::string &string)
| Creates a StringRef from a String.
| StringRef () noexcept
| Creates a StringRef pointer to an empty string.
| operator const String::CharPointerType::CharType * () const noexcept
| Returns a raw pointer to the underlying string data.
| operator String::CharPointerType () const noexcept
| Returns a pointer to the underlying string data as a char pointer object.
bool | isEmpty () const noexcept
| Returns true if the string is empty.
bool | isNotEmpty () const noexcept
| Returns true if the string is not empty.
int | length () const noexcept
| Returns the number of characters in the string.
juce_wchar | operator[] (int index) const noexcept
| Retrieves a character by index.
bool | operator== (const String &s) const noexcept
| Compares this StringRef with a String.
bool | operator!= (const String &s) const noexcept
| Compares this StringRef with a String.
bool | operator< (const String &s) const noexcept
| Compares this StringRef with a String.
bool | operator<= (const String &s) const noexcept
| Compares this StringRef with a String.
bool | operator> (const String &s) const noexcept
| Compares this StringRef with a String.
bool | operator>= (const String &s) const noexcept
| Compares this StringRef with a String.
bool | operator== (StringRef s) const noexcept
| Case-sensitive comparison of two StringRefs.
bool | operator!= (StringRef s) const noexcept
| Case-sensitive comparison of two StringRefs.

## Public Attributes

String::CharPointerType | text
| The text that is referenced.

## Constructors and Destructors

## StringRef() [1/5]

juce::StringRef::StringRef | ( | const char * | stringLiteral | ) |

noexcept

Creates a StringRef from a raw string literal.

The StringRef object does NOT take ownership or copy this data, so you must ensure that the data does not change during the lifetime of the StringRef. Note that this pointer cannot be null!

Referenced by operator!=(), and operator==().

## StringRef() [2/5]

juce::StringRef::StringRef | ( | String::CharPointerType | stringLiteral | ) |

noexcept

Creates a StringRef from a raw char pointer.

The StringRef object does NOT take ownership or copy this data, so you must ensure that the data does not change during the lifetime of the StringRef.

## StringRef() [3/5]

juce::StringRef::StringRef | ( | const String & | string | ) |

noexcept

Creates a StringRef from a String.

The StringRef object does NOT take ownership or copy the data from the String, so you must ensure that the String is not modified or deleted during the lifetime of the StringRef.

## StringRef() [4/5]

juce::StringRef::StringRef | ( | const std::string & | string | ) |

Creates a StringRef from a String.

The StringRef object does NOT take ownership or copy the data from the std::string, so you must ensure that the source string object is not modified or deleted during the lifetime of the StringRef.

## StringRef() [5/5]

juce::StringRef::StringRef | ( |  | ) |

noexcept

Creates a StringRef pointer to an empty string.

## Member Functions

## operator const String::CharPointerType::CharType *()

juce::StringRef::operator const String::CharPointerType::CharType * | ( |  | ) | const

inline noexcept

Returns a raw pointer to the underlying string data.

References operator String::CharPointerType(), and text.

## operator String::CharPointerType()

juce::StringRef::operator String::CharPointerType | ( |  | ) | const

inline noexcept

Returns a pointer to the underlying string data as a char pointer object.

References text.

Referenced by operator const String::CharPointerType::CharType *().

## isEmpty()

bool juce::StringRef::isEmpty | ( |  | ) | const

inline noexcept

Returns true if the string is empty.

References text.

## isNotEmpty()

bool juce::StringRef::isNotEmpty | ( |  | ) | const

inline noexcept

Returns true if the string is not empty.

References text.

## length()

int juce::StringRef::length | ( |  | ) | const

inline noexcept

Returns the number of characters in the string.

References text.

## operator[]()

juce_wchar juce::StringRef::operator[] | ( | int | index | ) | const

inline noexcept

Retrieves a character by index.

References text.

## operator==() [1/2]

bool juce::StringRef::operator== | ( | const String & | s | ) | const

inline noexcept

Compares this StringRef with a String.

References text.

## operator!=() [1/2]

bool juce::StringRef::operator!= | ( | const String & | s | ) | const

inline noexcept

Compares this StringRef with a String.

References text.

## operator<()

bool juce::StringRef::operator< | ( | const String & | s | ) | const

inline noexcept

Compares this StringRef with a String.

References text.

## operator<=()

bool juce::StringRef::operator<= | ( | const String & | s | ) | const

inline noexcept

Compares this StringRef with a String.

References text.

## operator>()

bool juce::StringRef::operator> | ( | const String & | s | ) | const

inline noexcept

Compares this StringRef with a String.

References text.

## operator>=()

bool juce::StringRef::operator>= | ( | const String & | s | ) | const

inline noexcept

Compares this StringRef with a String.

References text.

## operator==() [2/2]

bool juce::StringRef::operator== | ( | StringRef | s | ) | const

inline noexcept

Case-sensitive comparison of two StringRefs.

References StringRef(), and text.

## operator!=() [2/2]

bool juce::StringRef::operator!= | ( | StringRef | s | ) | const

inline noexcept

Case-sensitive comparison of two StringRefs.

References StringRef(), and text.

## Member Data Documentation

## text

String::CharPointerType juce::StringRef::text

The text that is referenced.

Referenced by juce::Rectangle< ValueType >::fromString(), isEmpty(), isNotEmpty(), length(), operator const String::CharPointerType::CharType *(), operator String::CharPointerType(), operator!=(), operator!=(), juce::operator+(), juce::operator+(), juce::operator+(), juce::operator+(), operator<(), operator<=(), operator==(), operator==(), operator>(), operator>=(), and operator[]().

### Purchase

Get JUCE

### Discover

What's New in JUCE Features

### Learn

Documentaion Tutorials Made with JUCE Resources

### Support

JUCE Forum Newsletter Archive

### About

Contact Us JUCE Legal JUCE Licensing FAQ

### Events

Audio Developer Conference
© Raw Material Software Limited