String str("abcdef"); - 완료
String str2("abcb");
String str3(" aB c ");
String str4(str); - 완료

함수 완수 여부
charAt str.charAt(3) == 'd' 완료
compareTo str2.compareTo("abcb") == 0
compareToIgnoreCase str2.compareTo("AbCb") == 0
concat str.concat(str) == "abcdefabcdef"
contains str.contains("bcd") == TRUE
endsWith str.endsWith("ef") == TRUE
startsWith str.startsWith("abc") == TRUE 완료
equals str.equals(new String("abcdef")) == TRUE 완료
equalsIgnoreCase str.equalsIgnoreCase(new String("ABcdEf")) == TRUE
indexOf str2.indexOf("b") == 1 완료
isEmpty str.isEmpty == FALSE 완료
lastIndexOf str2.lastIndexOf("b") == 3 완료
length str.length() == 6 완료
replace str.replace("bc", "ad") == "aaddef"
split str.split("c") == {"ab", "def"}
subString str.subString(2, 4) == "cd" 완료
format String::format("참석자 : %d명", 8) == "참석자 : 8명"
trim str3.trim() == "aB c" 완료
toLower str3.toLower() == " ab c "
toUpper str.toUpper() == "ABCDEF"
valueOf String::valueOf(3) == "3"


== 소스 코드 ===
#include <iostream>
using namespace std;

class String{
private:
	char* value;
	int offset;
	int count;
	int size;

public:
	String(){
		value = NULL;
		offset = 0;
		count = 0;
		size = 0;
	}

	String(const char* original){		
		// 길이 파악
		size = 0;
		for(;original[size] != '\0'; size++);
		count = size;
		
		// value에 데이터 복사
		value = new char[size];
		for(int i=0; i < size; i++)
			value[i] = original[i];

		offset = 0;
	}

	String(String& str, int o, int c){ 
		value = str.value;
		size = str.size;
		offset = o>=size? 0: o;
		count = c>size? size: c;
	}
	
	String subString(int off1, int off2){
		if(off1 < 0 || off2 < 0 || off1 > off2) return String();

		String str(*this, offset+off1, off2-off1);
		return str;
	}

	int length() { return size; }

	bool equals(String &str){
		if(count != str.count) return false;
		for(int i=0; i<count; i++){
			if(value[offset+i] != str.value[str.offset + i]) return false;
		}
		return true;
	}

	void print(){
		cout << "value : ";
		for(int i=0; i<size; i++)
			cout << value[i];
		
		cout << endl << "size : " << size << endl << "offset : " << offset << endl << "count : " << count  << endl;

		cout << "print : ";
		for(int i=0; i < count; i++)
			cout << value[offset+i];
		cout << endl << endl;
	}

	bool isEmpty(){ return (count == 0 || offset > size ); }
	
	int indexOf(char c){
		for(int i=0; i < count; i++)
			if(value[offset+i] == c) return i;
		return -1;
	}

	char charAt(int index){
		if(index > count || index < 0) return '\0';
		else return value[offset+index];
	}

	int lastIndexOf(char c){
		int index = -1;
		for(int i=0; i<count; i++)
			if(value[offset+i] == c) index = i; 
		return index;
	}

	bool startsWith(const char* original){
		for(int i=0; original[i] != '\0'; i++){
			if(i >= count) return false;
			if(original[i] != value[offset+i]) return false;
		}
		return true;
	}

	String concat(String &str){
		int length = count + str.count;
		char *nvalue = new char[length+1];
		for(int i=0; i<count; i++) nvalue[i] = value[offset+i];
		for(int i=0; i<str.count; i++) nvalue[count+i] = str.value[str.offset+i];
		nvalue[length] = '\0';

		return String(nvalue);
	}

	String trim(){
		int firstIndex = offset;
		int lastIndex = offset+count-1;
		
		for(int i=firstIndex; i<count; i++){
			if(value[i] != ' '){
				firstIndex = i;
				break;
			}
		}
		for(int i=lastIndex; i >= offset; i--){
			if(value[i] != ' '){
				lastIndex = i+1;
				break;
			}
		}
		
		return String(*this, firstIndex, lastIndex-firstIndex);
	}


};


int main(void){

	String str1("abcdef");
	str1.print();

	String str2(str1, 3, 2);
	str2.print();

	String str3 = str1.subString(3, 5);
	str3.print();

	String str4("abcb");
	str4.print();

	String str5 = str1.concat(str1);
	str5.print();

	String str6("           ab e       ");
	str6.print();
	str6 = str6.trim();
	str6.print();

	String str7("abcdefgh");
	String str8("de");

	//cout << str7.subString(2, 6).subString(1, 3).equals(str8) << endl;

	cout << "str2 == str3? : " << str2.equals(str3) << endl;
	cout << "str1 indexOf b : " << str1.indexOf('b') << endl;
	cout << "str1 charAt 1 : " << str1.charAt(3) << endl;
	cout << "str4 lastIndexOf b : " << str4.lastIndexOf('b') << endl;
	cout << "str1 startsWith abc? : " << str1.startsWith("abc") << endl;
	cout << "str4 startsWith abc? : " << str4.startsWith("abc") << endl;
	cout << "str2 startsWith abc? : " << str2.startsWith("abc") << endl;
	
	

	//printf("%d", '0');

	return 0;
}

Retrieved from http://wiki.zeropage.org/wiki.php/데블스캠프2011/셋째날/String만들기/송지원
last modified 2021-02-07 05:29:14