Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Source] Java-like String object
Index -> Programming, C++ -> C++ Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
rizzix




PostPosted: Thu Sep 04, 2003 4:44 pm   Post subject: [Source] 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 << str << endl;

3] the toCharArray will return a pointer to a new char array, so delete it when not necessary.

4] the string object is immutable, all methods are const

5] basic primitive types are returned by value

6] all methods returning an int or char, return -1 if error, except for the compareTo method and it's variations.

7] the indexOf methods that take a String or char* as an argument are NOT IMPLEMENTED

8] methods that take String as an argument will tend to have a better performance gain. but it is subtle.

9] class is designed to take advantage of the Digital Mars' garbage collector

10] most importantly this class is cross-platform compatible



JString.zip
 Description:
June 19, 2004

Download
 Filename:  JString.zip
 Filesize:  6.33 KB
 Downloaded:  283 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Catalyst




PostPosted: Thu Sep 04, 2003 6:51 pm   Post subject: (No subject)

very nice

but why not just use std::string?
Catalyst




PostPosted: Thu Sep 04, 2003 7:25 pm   Post subject: (No subject)

heres some useful things to add
code:

    String& operator=(const String& pstr);
    String& operator=(const char* const pstr);
    String& operator+(const String& pstr) const;
    String& operator+(const char* const pstr) const;
    String& operator+=(const String& pstr);
    String& operator+=(const char* const pstr);
   
    friend std::ostream & operator << (std::ostream & outs, String &s);


code:

String& String::operator+(const String& pstr) const
{
    return concat (pstr);

}
String& String::operator+(const char* const pstr) const
{
    return concat (pstr);
}

String& String::operator+=(const String& pstr)
{
    String hold=concat(pstr);
   
    delete [] str;
    str = hold.toCharArray();
    len = hold.length() + 1;
   
    return *this;
}
String& String::operator+=(const char* const pstr)
{
    String hold=concat(pstr);
   
    delete [] str;
    str = hold.toCharArray();
    len = hold.length() + 1;
   
    return *this;
}

char String::operator[] (int index) const
{
    if (index >= length() || index < 0)
        return -1;
    else
        return str[index];

}

std::ostream & operator << (std::ostream & outs, String &s)
{
  outs << s.toString() << std::endl;
  return outs;
}
rizzix




PostPosted: Thu Sep 04, 2003 10:15 pm   Post subject: (No subject)

cool. nice additions. so thats how you implement the << operator.
rizzix




PostPosted: Sat Jun 19, 2004 1:35 am   Post subject: (No subject)

subtle update.. for those interested.
wtd




PostPosted: Sat Jun 19, 2004 2:58 am   Post subject: (No subject)

I agree that it's silly to use something like this as opposed to the std::string class. However, if you're going to do this, you should include const qualifiers in places like:

code:
friend std::ostream& operator<<(std::ostream& out, const String& s);
rizzix




PostPosted: Sat Jun 19, 2004 11:25 am   Post subject: (No subject)

oh but they are in place.
wtd




PostPosted: Sat Jun 19, 2004 1:31 pm   Post subject: (No subject)

Sorry. Somehow I missed that. That'll teach me to post when I'm half asleep.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, C++ -> C++ Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: