Author |
Message |
rizzix
|
Posted: 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
Description: |
|
Download |
Filename: |
JString.zip |
Filesize: |
6.33 KB |
Downloaded: |
500 Time(s) |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Catalyst
|
Posted: Thu Sep 04, 2003 6:51 pm Post subject: (No subject) |
|
|
very nice
but why not just use std::string?
|
|
|
|
|
|
Catalyst
|
Posted: 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
|
Posted: Thu Sep 04, 2003 10:15 pm Post subject: (No subject) |
|
|
cool. nice additions. so thats how you implement the << operator.
|
|
|
|
|
|
rizzix
|
Posted: Sat Jun 19, 2004 1:35 am Post subject: (No subject) |
|
|
subtle update.. for those interested.
|
|
|
|
|
|
wtd
|
Posted: 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
|
Posted: Sat Jun 19, 2004 11:25 am Post subject: (No subject) |
|
|
oh but they are in place.
|
|
|
|
|
|
wtd
|
Posted: 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
|
|
|
|