
-----------------------------------
rizzix
Thu Sep 04, 2003 4:42 pm

Java-like String object
-----------------------------------
here's something useful, a java.lang.String object that you can use.
some things to remember while using this class.

1] any method that returns a String& reference, is basically returning a new object, so you'll have to delete it when not necessary

2] to print the string to terminal do this: cout  length() || fromIndex < 0)
        return -1;
    
    int result = -1;
    
    for (int i = fromIndex; i < length(); i++)
        if (charAt(i) == ch) {
            result = i;
            break;
        }
        
    return result;
}

/*TODO: implement this
// returns the index of the substring pstr in the string, -1 if error
int String::indexOf(const String& pstr) const
{
    
}


int String::indexOf(const char* const pstr) const
{
    
}


// same as indexOf for substrings but from fromIndex onwards
int String::indexOf(const char* const pstr, int fromIndex) const
{
    
}


int String::indexOf(const String& pstr, int fromIndex) const
{
    
}
*/

// returns the index of char in the string,
// the string is read backwards. (this index is given as though you
// were reading the string forward, not backwards)
int String::lastIndexOf(char ch) const
{
    return lastIndexOf(ch, length() - 1);
}


int String::lastIndexOf(char ch, int fromIndex) const
{
    if (fromIndex > length() || fromIndex < 0)
        return -1;
    
    int result = -1;
    
    for (int i = fromIndex; i >= 0; i--)
        if (charAt(i) == ch) {
            result = i;
            break;
        }
        
    return result;
}

/*TODO: implement this
int String::lastIndexOf(const char* const pstr) const
{
    
}


int String::lastIndexOf(const char* const pstr, int fromIndex) const
{
    
}


int String::lastIndexOf(const String& pstr) const
{
    
}


int String::lastIndexOf(const String& pstr, int fromIndex) const
{
    
}
*/


// returns a new String that is a substring from beginIndex onwards,
// null if error
String* String::substring(int beginIndex) const
{
    return substring(beginIndex, length() - 1);
}


// returns a new String that is a substring from beginIndex to endIndex,
// null if error
String* String::substring(int beginIndex, int endIndex) const
{
    if (beginIndex > endIndex || beginIndex < 0 || beginIndex > length()) 
        return (String*) 0;
    if (endIndex < beginIndex || endIndex < 0 || endIndex > length())
        return (String*) 0;

    int pLength = endIndex - beginIndex + 2;  // allocate space for '\0' aswell
    char tmpStr[pLength];      
    
    for (int i = beginIndex; i = 97 && charAt(i) = 65 && charAt(i) = 0; i--)
        if (charAt(i) != ' ') {         // dot = space
            end = i + 1;                // "....test...."
            break;                      //          ^-- where end points to
        }
      
    int newLength = (end - begin) + 1;
    
    if (end > begin) {
        char tmpStr[newLength];
    
        for (int i = 0; i < newLength; i++)
            tmpStr[i] = charAt(begin + i);
            
        tmpStr[newLength - 1] = '\0';
        
        return *(new String(tmpStr));
    } else {                            // if end = length() || index < 0)
        return -1;
    else
        return str[index];
}


// returns a new string that has the same contents of this String object
// with oldChar replaced with newChar
String& String::replace(char oldChar, char newChar) const
{
    char tmpStr[length() + 1];
    
    for (int i = 0; i < length(); i++) {
        if (charAt(i) == oldChar)
            tmpStr[i] = newChar;
        else
            tmpStr[i] = charAt(i);
    }
    tmpStr[length()] = '\0';
    
    return *(new String(tmpStr));
}


// overloaded operators
String& String::operator=(const String& pstr)
{
    delete [] str;
    str = pstr.toCharArray();
    len = pstr.length() + 1;
    
    return *this;
}


String& String::operator=(const char* const pstr)
{
    delete [] str;
    len = 0;
    while (true) {
        if (pstr != 0 && *(pstr + len) != '\0') {
            len++;
        } else {
            break;
        }
    }
    
    len += 1;                                   // allocate space for '\0'
    
    if (len != 0) {
        str = new char[len];
        for (int i = 0; i < len; i++)
            str[i] = pstr[i];
        str[len - 1] = '\0';
    }
    return *this;
}

