objc

Instance Methods | Class Methods
NXString Class Reference

A class for managing and manipulating string objects. More...

#import <Foundation/Foundation.h>

Inheritance diagram for NXString:
Inheritance graph
[legend]
Collaboration diagram for NXString:
Collaboration graph
[legend]

Instance Methods

(id- initWithString:
 Initializes a new string by referencing another string. More...
 
(id- initWithCapacity:
 Initializes a new empty mutable string with pre-allocated capacity. More...
 
(id- initWithCString:
 Initializes a new string by referencing a constant c-string. More...
 
(id- initWithFormat:
 Initializes a new string with a formatted string. More...
 
(id- initWithFormat:arguments:
 Initializes a new string with a formatted string and arguments. More...
 
(const char *) - cStr
 Returns the C-string representation of the string. More...
 
(unsigned int) - length
 Returns the length of the string. More...
 
(void *) - bytes
 Returns the mutable C-string representation of the string. More...
 
(size_t) - capacity
 Returns the capacity of the string. More...
 
(BOOL- append:
 Appends another string to this string. More...
 
(BOOL- appendCString:
 Appends a null-terminated C-string to this string. More...
 
(BOOL- appendStringWithFormat:
 Appends a string with format and arguments to this string. More...
 
(BOOL- trimWhitespace
 Trims leading and trailing whitespace from the string. More...
 
(BOOL- trimPrefix:suffix:
 Trims leading and trailing string values from the string. More...
 
(BOOL- hasPrefix:
 Checks if the string starts with a given prefix. More...
 
(BOOL- hasSuffix:
 Checks if the string ends with a given suffix. More...
 
(unsigned int) - countOccurrencesOfByte:
 Counts the number of occurrences of a byte character. More...
 
(NXString *) - JSONString
 Returns a quoted version of the string. More...
 
(BOOL- containsString:
 Checks if the string contains a given substring. More...
 
(NXComparisonResult- compare:
 Compares this string with another string. More...
 
(BOOL- toUppercase
 Converts the string to uppercase. More...
 
(BOOL- toLowercase
 Converts the string to lowercase. More...
 
- Instance Methods inherited from NXObject
(id- retain
 Increases the retain count of the receiver. More...
 
(void) - release
 Decreases the retain count of the receiver. More...
 
(id- autorelease
 Adds the receiver to the autorelease pool.
 
- Instance Methods inherited from Object
(void) - dealloc
 Free resources for an existing instance.
 
(id- init
 Initialize the instance, after allocation. More...
 
(Class- class
 Returns the class of the instance. More...
 
(Class- superclass
 Returns the superclass of the instance. More...
 
(BOOL- isEqual:
 Compares the receiver to another object for equality. More...
 
(BOOL- isKindOfClass:
 Returns a Boolean value that indicates whether the receiver is an instance of a given class. More...
 
(BOOL- conformsTo:
 Checks if the receiver's class conforms to a protocol. More...
 
(NXString *) - description
 Returns a string that represents the instance. More...
 
- Instance Methods inherited from <ObjectProtocol>
(BOOL- respondsToSelector:
 Checks if the receiver responds to a selector. More...
 
- Instance Methods inherited from <JSONProtocol>
(size_t) - JSONBytes
 Returns the appropriate capacity for the JSON representation of the instance. More...
 

Class Methods

(NXString *) + new
 Return a new empty string.
 
(NXString *) + stringWithCapacity:
 Create a new empty string with pre-allocated capacity. More...
 
(NXString *) + stringWithString:
 Return a string by referencing another string.
 
(NXString *) + stringWithCString:
 Return a string by referencing a c-string.
 
(NXString *) + stringWithFormat:
 Return a string from format string and arguments.
 
- Class Methods inherited from NXObject
(id+ allocWithZone:
 Allocates a new instance of an object in a specific memory zone. More...
 
- Class Methods inherited from Object
(void) + initialize
 Performs one-time initialization for the class. More...
 
(id+ alloc
 Allocate a new class instance. More...
 
(Class+ class
 Returns the class object. More...
 
(Class+ superclass
 Returns the superclass of the class. More...
 
(const char *) + name
 Returns the name of the class. More...
 
(BOOL+ conformsTo:
 Checks if the class conforms to a protocol. More...
 
(NXString *) + description
 Returns a string that represents the class. More...
 

Additional Inherited Members

- Protected Attributes inherited from NXObject
id _zone
 The memory zone where the object is allocated.
 
unsigned short _retain
 The retain count of the object.
 
id _next
 The next object in an autorelease pool.
 
- Protected Attributes inherited from Object
Class isa
 A pointer to the object's class structure. More...
 

Detailed Description

A class for managing and manipulating string objects.

NXString provides functionality for creating and working with text strings. It supports both mutable and immutable string representations.

Definition at line 20 of file NXString.h.

Method Documentation

◆ append:()

- (BOOL) append: (id< NXConstantStringProtocol >)  other

Appends another string to this string.

Parameters
otherThe string to append.
Returns
YES if successfully modified, NO if it wasn't possible to allocate additional capacity.

◆ appendCString:()

- (BOOL) appendCString: (const char *)  other

Appends a null-terminated C-string to this string.

Parameters
otherThe C-string to append.
Returns
YES if successfully modified, NO if it wasn't possible to allocate additional capacity.

◆ appendStringWithFormat:()

- (BOOL) appendStringWithFormat: (NXConstantString *)  format
,   ... 

Appends a string with format and arguments to this string.

Parameters
formatThe format string to use for appending.
...Variable arguments for the format string.
Returns
YES if successfully modified, NO if it wasn't possible to allocate additional capacity.

◆ bytes()

- (void *) bytes

Returns the mutable C-string representation of the string.

Returns
A pointer to a null-terminated C-string representing the string content. Returns NULL if it the string cannot be made mutable.

If the string is immutable, this method makes the string mutable and returns a pointer to the internal buffer. If the string is already mutable, it simply returns the pointer to the internal buffer.

◆ capacity()

- (size_t) capacity

Returns the capacity of the string.

Returns
The total allocated size of the string buffer, including the null terminator.

This method returns the current capacity of the string's internal buffer, which may be larger than the actual length of the string. This is useful for understanding how much space is available for appending or modifying the string without requiring reallocation. If zero is returned, it indicates that the string is currently immutable.

◆ compare:()

- (NXComparisonResult) compare: (id< NXConstantStringProtocol >)  other

Compares this string with another string.

Parameters
otherThe string to compare against.
Returns
NXComparisonResult indicating whether this string is earlier (Ascending), Same, or later (Descending) than the other string, according to lexicographical order.

◆ containsString:()

- (BOOL) containsString: (id< NXConstantStringProtocol >)  other

Checks if the string contains a given substring.

Parameters
otherThe NXConstantString or NXString instance to check for containment.
Returns
YES if the string contains the specified substring, NO otherwise.

◆ countOccurrencesOfByte:()

- (unsigned int) countOccurrencesOfByte: (uint8_t)  ch

Counts the number of occurrences of a byte character.

Parameters
chThe character to count occurrences of.
Returns
The number of times the character appears in the string.

◆ cStr()

- (const char *) cStr

Returns the C-string representation of the string.

Returns
A pointer to a null-terminated C-string representing the string content.

This method retrieves the C-string representation of the string, which can be used in C-style string operations.

Reimplemented from <NXConstantStringProtocol>.

◆ hasPrefix:()

- (BOOL) hasPrefix: (id< NXConstantStringProtocol >)  prefix

Checks if the string starts with a given prefix.

Parameters
prefixThe NXConstantString or NXString instance to check as a prefix.
Returns
YES if the string starts with the specified prefix, NO otherwise.

◆ hasSuffix:()

- (BOOL) hasSuffix: (id< NXConstantStringProtocol >)  suffix

Checks if the string ends with a given suffix.

Parameters
suffixThe NXConstantString or NXString instance to check as a suffix.
Returns
YES if the string ends with the specified suffix, NO otherwise.

◆ initWithCapacity:()

- (id) initWithCapacity: (size_t)  capacity

Initializes a new empty mutable string with pre-allocated capacity.

Parameters
capacityThe initial capacity (in bytes) to allocate for the string buffer. This should include space for the null terminator. If 0 is passed, no memory is allocated and the string starts empty.
Returns
The initialized string object with the specified capacity allocated.
Note
This method creates a mutable string with internal storage pre-allocated to the specified capacity. The string starts empty but has the capacity to grow up to the specified size without requiring memory reallocation.
Warning
If memory allocation fails, this method will return nil.

◆ initWithCString:()

- (id) initWithCString: (const char *)  cStr

Initializes a new string by referencing a constant c-string.

Parameters
cStrA null-terminated C-string to create the string from.
Returns
A new NXString instance containing the C-string content.

◆ initWithFormat:()

- (id) initWithFormat: (NXConstantString *)  format
,   ... 

Initializes a new string with a formatted string.

Parameters
formatThe format string to use for initialization.
...Variable arguments for the format string.
Returns
A new NXString instance containing the formatted content.

◆ initWithFormat:arguments:()

- (id) initWithFormat: (NXConstantString *)  format
arguments: (va_list)  args 

Initializes a new string with a formatted string and arguments.

Parameters
formatThe format string to use for initialization.
argsA va_list containing the arguments for the format string.
Returns
A new NXString instance containing the formatted content.

This initializer is used internally to initialize the string with a format and variable arguments. It is not intended to be called directly.

◆ initWithString:()

- (id) initWithString: (id< NXConstantStringProtocol, ObjectProtocol >)  other

Initializes a new string by referencing another string.

This method retains the source string, and if modified, it will create a mutable copy of the modified string whilst releasing the original reference.

Parameters
otherThe source string (either NXConstantString or NXString) to retain.
Returns
The initialized string object.

◆ JSONString()

- (NXString *) JSONString

Returns a quoted version of the string.

Returns
A new NXString instance containing the string enclosed in double quotes.

This method creates a new string that contains the original string enclosed in double quotes. Special characters within the string are escaped according to JSON string escaping rules.

Reimplemented from <JSONProtocol>.

◆ length()

- (unsigned int) length

Returns the length of the string.

Returns
The number of bytes in the string.

This method returns the length of the string, in bytes, excluding the null terminator.

Reimplemented from <NXConstantStringProtocol>.

◆ stringWithCapacity:()

+ (NXString *) stringWithCapacity: (size_t)  capacity

Create a new empty string with pre-allocated capacity.

Parameters
capacityThe initial capacity (in bytes) to allocate for the string buffer. This should include space for the null terminator.
Returns
A new autoreleased NXString instance with the specified capacity allocated.

This method creates a mutable string with internal storage pre-allocated to the specified capacity. This is useful when you plan to modify the string (e.g., with append operations) and want to avoid multiple reallocations. The string starts empty but has the capacity to grow up to the specified size without requiring memory reallocation.

Note
The capacity represents the total buffer size including the null terminator, so a capacity of 10 allows for 9 characters plus the null terminator, for example.
Warning
If memory allocation fails, this method will return nil.

◆ toLowercase()

- (BOOL) toLowercase

Converts the string to lowercase.

Returns
YES if the string was modified to lowercase, NO if the string was not modified (e.g., it is already lowercase or unable to make mutable).

This method modifies the string in place, converting all alphabetic characters to their lowercase equivalents.

◆ toUppercase()

- (BOOL) toUppercase

Converts the string to uppercase.

Returns
YES if the string was modified to uppercase, NO if the string was not modified (e.g., it is already uppercase or unable to make mutable).

This method modifies the string in place, converting all alphabetic characters to their uppercase equivalents.

◆ trimPrefix:suffix:()

- (BOOL) trimPrefix: (id< NXConstantStringProtocol >)  prefix
suffix: (id< NXConstantStringProtocol >)  suffix 

Trims leading and trailing string values from the string.

Parameters
prefixThe NXConstantString or NXString instance to trim from the start, or NULL to skip.
suffixThe NXConstantString or NXString instance to trim from the end, or NULL to skip.
Returns
YES if the string was modified, NO if it was already trimmed.

◆ trimWhitespace()

- (BOOL) trimWhitespace

Trims leading and trailing whitespace from the string.

Returns
YES if the string was modified, NO if it was already trimmed.

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