标签:des style blog ar io color os sp for
1 //Strings.cpp 2 3 /* 4 Copyright 2000-2004 The VCF Project. 5 Please see License.txt in the top level directory 6 where you installed the VCF. 7 */ 8 9 10 #include "vcf/FoundationKit/FoundationKit.h" 11 12 /** 13 *use the VCF namespace to make it more convenient 14 *to refer to VCF classes 15 */ 16 using namespace VCF; 17 18 19 20 /** 21 Sample object that demonstrates overloading the toString method of the 22 Object base class. 23 */ 24 class Foo : public Object { 25 public: 26 virtual String toString() { 27 return String("Foo here!\n\t") + Object::toString(); 28 } 29 }; 30 31 32 33 34 /** 35 This example show how to work with strings 36 in the VCF and some of the extra utility functions in the StringUtils class. 37 Many thanks to Cesar Mello for doing the initial work on this 38 */ 39 int main(int argc, char *argv[]) 40 { 41 FoundationKit::init( argc, argv ); 42 43 try { 44 45 // creating strings 46 String s = "Now"; 47 String t = s + " is the time."; // concatenate strings with + operator 48 49 /** 50 use System::println() to send the String to stdout 51 */ 52 System::println(t); 53 54 int len = t.size(); // number of characters: 16 55 System::println(Format("Number of characters: %d") % len); 56 57 String sub = t.substr(4); // returns char 4 to end: "is the time." 58 System::println("substr(4): " + sub); 59 60 sub = t.substr(4, 2); // returns chars 4 and 5 61 System::println("substr(4, 2): " + sub); 62 63 String toFind = "foo"; 64 int pos = t.find(toFind); // does not find the string (returns String::npos) 65 66 if (pos == UnicodeString::npos) 67 { 68 System::println( Format("trying to find string \"foo\": not found. returns String::npos: %d") % pos); 69 } 70 71 System::println( toFind ); 72 73 toFind = "time"; 74 75 System::println( toFind ); 76 77 pos = t.find("time"); // finds the string "time" in position 11 78 System::println( Format("string \"time\" found at position: %d in string {%s}") % pos % t ); 79 80 81 82 83 Foo f; 84 System::println( String("f is: ") + f + "\n" + 99.5643231 + " bottles of " + &f + " on the wall!" ); 85 86 System::println( String("Is this ") + true + " or is this " + false + "?" ); 87 88 DateTime dt = DateTime::now(); 89 90 System::println( "The time is now: " + dt.toString() ); 91 92 void* ptr = &dt; 93 94 System::println( String("The value of the void* pointer ptr is: ") + ptr ); 95 96 97 int i = 0; 98 bool j = false; 99 const double* k = NULL; 100 float l = 0; 101 Object o; 102 103 System::println( "i is a " + typeid(i) + " type" ); 104 System::println( "j is a " + typeid(j) + " type" ); 105 System::println( "k is a " + typeid(k) + " type" ); 106 System::println( "l is a " + typeid(l) + " type" ); 107 System::println( "o is a " + typeid(o) + " type" ); 108 System::println( "f is a " + typeid(f) + " type" ); 109 System::println( "ptr is a " + typeid(ptr) + " type" ); 110 System::println( "dt is a " + typeid(dt) + " type" ); 111 112 int64 m = 0xffffffff; 113 System::println( String("m: ") + m ); 114 115 m *= -0xfff; 116 117 System::println( String("m: ") + m ); 118 119 120 VariantData v; 121 v = 123; 122 123 System::println( String("Variant (as int): ") + v ); 124 125 v = 441.2340995; 126 System::println( String("Variant (as double): ") + v ); 127 128 v = &f; 129 System::println( String("Variant (as object): ") + v ); 130 131 v = &dt; 132 System::println( String("Variant (as object): ") + v ); 133 134 v = true; 135 System::println( String("Variant (as boolean): ") + v ); 136 137 v = -199; 138 System::println( String("Variant (as signed int): ") + v ); 139 140 v = ‘A‘; 141 System::println( String("Variant (as character): ") + v ); 142 143 v = "Hello World!"; 144 System::println( String("Variant (as string): ") + v ); 145 146 /** 147 String utility functions 148 */ 149 150 /** 151 format a string 152 */ 153 String formattedString = Format( "hello %d, %0.2f World!!" ) % 1002 % 12.456330; 154 System::println( formattedString ); 155 156 //error - too many arguments, expecting 2 argument, recv‘d 3! 157 try { 158 formattedString = Format( "hello %d, %0.2f World!!" ) % 1002 % 0.0786 % 456; 159 System::println( formattedString ); 160 } 161 catch ( std::exception& e ) { 162 System::print( Format("%s\n") % e.what() ); 163 } 164 165 166 //error - too few arguments, expecting 2 argument, recv‘d 1! 167 try { 168 formattedString = Format( "hello %d, %0.2f World!!" ) % 1002; 169 System::println( formattedString ); 170 } 171 catch ( std::exception& e ) { 172 System::print( Format("%s\n") % e.what() ); 173 } 174 175 //StringUtils::format is deprecated - don‘t use in new code 176 formattedString = Format( "Number: %d, as hex: 0x%08X, a string: %s" ) % 12 % 12 % toFind; 177 System::println( formattedString ); 178 179 180 //same thing with a Format 181 formattedString = Format( "Number: %d, as hex: 0x%08X, a string: %s") % 12 % 12 % toFind; 182 System::println( formattedString ); 183 184 /** 185 code added with only purpose of fulling testing the Format functionality 186 */ 187 s = Format( "abc" ); 188 VCF_ASSERT( "abc" == s ); 189 190 s = Format( "%%" ); 191 VCF_ASSERT( "%" == s ); 192 193 s = Format( "%d" ) % 1; 194 VCF_ASSERT( "1" == s ); 195 196 s = Format( "%d%%" ) % 1; 197 VCF_ASSERT( "1%" == s ); 198 199 s = Format( "a %d%%" ) % 1; 200 VCF_ASSERT( "a 1%" == s ); 201 202 s = Format( "%% %d%%" ) % 1; 203 VCF_ASSERT( "% 1%" == s ); 204 205 s = Format( "a %% %% %d" ) % 1; 206 VCF_ASSERT( "a % % 1" == s ); 207 208 s = Format( "%da %% %% %d" ) % 1 % 2; 209 VCF_ASSERT( "1a % % 2" == s ); 210 211 s = Format( "%da %% %% %d%%" ) % 1 % 2; 212 VCF_ASSERT( "1a % % 2%" == s ); 213 214 s = Format( "%da %% %% %d%%%d" ) % 1 % 2 % 3; 215 VCF_ASSERT( "1a % % 2%3" == s ); 216 217 s = Format( "%da %% %% %d%%%d%%" ) % 1 % 2 % 3; 218 VCF_ASSERT( "1a % % 2%3%" == s ); 219 220 s = Format( "%da %% %% %d%%%%%d%%" ) % 1 % 2 % 3; 221 VCF_ASSERT( "1a % % 2%%3%" == s ); 222 223 s = Format("Hola from %s") % String("me"); 224 VCF_ASSERT( "Hola from me" == s ); 225 226 s = Format("Hola from %s%%") % String("me"); 227 VCF_ASSERT( "Hola from me%" == s ); 228 229 s = Format( "a %d \r\n" ) % 1 ; 230 VCF_ASSERT( "a 1 \r\n" == s ); 231 232 s = Format( "a %5d \r\n" ) % 1 ; 233 VCF_ASSERT( "a 1 \r\n" == s ); 234 235 s = Format( "a %d --> %d is %d .. %d \r\n" ) % 1 % 2 % 3 % 4; 236 VCF_ASSERT( "a 1 --> 2 is 3 .. 4 \r\n" == s ); 237 238 239 //error - too many arguments, expecting 2 argument, recv‘d 3! 240 try { 241 s = Format( "%%d" ) % 1; 242 } 243 catch ( std::exception& e ) { 244 System::print( Format("%s\n") % e.what() ); 245 } 246 247 s = Format( "%d" ) %1; 248 249 try { 250 s = Format( "%d" ); 251 } 252 catch ( std::exception& e ) { 253 System::print( Format("%s\n") % e.what() ); 254 } 255 256 try { 257 s = Format( "%d %d" ) % 1; 258 } 259 catch ( std::exception& e ) { 260 System::print( Format("%s\n") % e.what() ); 261 } 262 263 264 265 266 267 268 /** 269 getting a class name from type info 270 */ 271 272 String className = StringUtils::getClassNameFromTypeInfo( typeid(double) ); 273 274 System::println( Format("StringUtils::getClassNameFromTypeInfo() returned: %s") % className ); 275 276 className = StringUtils::getClassNameFromTypeInfo( typeid(StringUtils) ); 277 System::println( Format("StringUtils::getClassNameFromTypeInfo() returned: %s") % className ); 278 279 className = StringUtils::getClassNameFromTypeInfo( typeid(System) ); 280 System::println( Format("StringUtils::getClassNameFromTypeInfo() returned: %s") % className ); 281 282 283 284 /** 285 transform a string to and from upper case and lowercase 286 */ 287 288 String xfrmedString = StringUtils::lowerCase( className ); 289 290 System::println( Format("lowercase: %s") % xfrmedString ); 291 292 xfrmedString = StringUtils::upperCase( className ); 293 294 System::println( Format("uppercase: %s") % xfrmedString ); 295 296 297 /** 298 This will generate a new UUID and return it as a string 299 */ 300 String newUUID = StringUtils::newUUID(); 301 302 System::println( Format("new UUID: %s") % newUUID ); 303 304 305 /** 306 converts various non string types to a string 307 */ 308 309 String val = StringUtils::toString( 12 ); 310 System::println( Format("value: %s") % val ); 311 312 val = StringUtils::toString( 1234.009459034 ); 313 System::println( Format("value: %s") % val ); 314 315 val = StringUtils::toString( 53433.000034f ); 316 System::println( Format("value: %s") % val ); 317 318 val = StringUtils::toString( true ); 319 System::println( Format("value: %s") % val ); 320 321 val = StringUtils::toString( false ); 322 System::println( Format("value: %s") % val ); 323 324 325 /** 326 A trace statement useful for debugging. On Win32 systems this goes to the 327 debugger for the current application 328 */ 329 330 StringUtils::trace( "Hello World" ); 331 332 /** 333 Same as above, only with variable arguments 334 */ 335 StringUtils::trace( Format("Hello World %d times") % 10 ); 336 337 338 /** 339 This demonstrates trimming text from a string 340 */ 341 String original = "####Some text#######"; 342 343 xfrmedString = StringUtils::trim( original, ‘#‘ ); 344 System::println( Format("original: %s \nxfrmedString after StringUtils::trim(): %s") % original % xfrmedString ); 345 346 347 xfrmedString = StringUtils::trimLeft( original, ‘#‘ ); 348 System::println( Format("original: %s \nxfrmedString after StringUtils::trimLeft(): %s") % original % xfrmedString ); 349 350 xfrmedString = StringUtils::trimRight( original, ‘#‘ ); 351 System::println( Format("original: %s \nxfrmedString after StringUtils::trimRight(): %s") % original % xfrmedString ); 352 353 354 /** 355 The next examples uses the white space trimming functions 356 */ 357 358 original = " 12333873 "; 359 360 xfrmedString = original; 361 StringUtils::trimWhiteSpaces( xfrmedString ); 362 System::println( Format("original: \"%s\"\nxfrmedString after StringUtils::trimWhiteSpaces(): %s") % original % xfrmedString ); 363 364 xfrmedString = original; 365 StringUtils::trimWhiteSpacesLeft( xfrmedString ); 366 System::println( Format("original: \"%s\"\nxfrmedString after StringUtils::trimWhiteSpacesLeft(): \"%s\"") % original % xfrmedString ); 367 368 369 xfrmedString = original; 370 StringUtils::trimWhiteSpacesRight( xfrmedString ); 371 System::println( Format("original: \"%s\"\nxfrmedString after StringUtils::trimWhiteSpacesRight(): \"%s\"") % original % xfrmedString ); 372 373 374 System::println( "." ); 375 System::println( "all the tests and the example have completed successfully" ); 376 } 377 catch ( std::exception& e ) { 378 System::print( e.what() ); 379 } 380 381 382 FoundationKit::terminate(); 383 384 return 0; 385 } 386 387 388 389 390 namespace VCF { 391 392 393 394 395 }; 396 397 398 /** 399 $Id: Strings.cpp 2818 2006-07-03 20:41:43Z kdmix $ 400 */
标签:des style blog ar io color os sp for
原文地址:http://www.cnblogs.com/elitiwin/p/4171803.html