home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / basedemo / string / strtest.cxx < prev   
C/C++ Source or Header  |  1995-02-26  |  4KB  |  118 lines

  1.  
  2.  
  3. #include "base/base.h"
  4.  
  5. #include <stdio.h>
  6. #include <strstream.h>
  7. #include <iostream.h>
  8.  
  9. void main ()
  10. {
  11.     {
  12.         CL_String line ("A sample, with a comma,  here. ");
  13.         CL_StringSequence sq = line.Split (' ');
  14.         long n = sq.Size();
  15.         cout << "# fields: " << n << endl;
  16.         for (short i = 0; i < n; i++)
  17.             cout << "'" << sq[i] << "'\n" ;
  18.         CL_String f1 = line.Field (1); // f1 now contains "A"
  19.         f1 = line.Field (2, " ,");     // f1 now contains "sample"
  20.         f1 = line.Field (2, ",");      // f1 now contains " with a comma"
  21.         CL_String fld[3];
  22.         n = line.Split (fld, 3, " ,");
  23.         // Now fld[0] contains "A"
  24.         //     fld[1] contains "sample"
  25.         //     fld[2] contains ", with a comma, here."
  26.         //     n contains 3.
  27.         i = 1;
  28.     }
  29.     {
  30.         CL_String s ("This string has five words.");
  31.         CL_StringSplitter split (s);
  32.         CL_String field;
  33.         split.Reset();
  34.         field = split.Next ();     // Use blank as separator, so
  35.                                    // field now contains "This"
  36.         field = split.Next ("i");  // field is now " str", and the
  37.                                    // separator set becomes "i"
  38.         field = split.Next ();     // field is now "ng has f", since "i"
  39.                                    // is the current separator set
  40.     }
  41.     istrstream st1 ("Line 1\nLine 02\n Line 3\n 12:30");
  42.     CL_String s1;
  43.     s1.ReadLine (st1);
  44.     cout << s1 << endl;
  45.     s1.ReadLine (st1);
  46.     cout << s1 << endl;
  47.     s1.ReadLine (st1);
  48.     cout << s1 << endl;
  49.     CL_TimeOfDay time1;
  50.     st1 >> time1;
  51.     cout << time1 << endl;
  52.     
  53.     CL_String strg1;
  54.  
  55.     strg1.Insert (" Hello ABC ");
  56.     strg1.Insert ('W', 10);
  57.     strg1.Insert (" 123456789 123456789 123456789 12345678 ", 5);
  58.     strg1.Append ('!');
  59.     
  60.     strg1 = 9879873L; cout << "After long assignment: " << strg1 << endl;
  61.     strg1 = 8989.893; cout << "After float assignment: " << strg1 << endl;
  62.  
  63.     CL_String s (5003L, 10);
  64.     CL_String t (-51L, 4);
  65.     CL_String u (3L, 2);
  66.     CL_String v (0L, 3);
  67.  
  68.     v.AssignWithFormat ("String '%s' long %ld char '%c'", strg1.AsPtr(),
  69.                         strg1.AsLong(), 'x');
  70.     CL_String line (" A line where \"Hello, world\" is included");
  71.     //    printf (" 'where' occurs at position %ld\n", line.Index ("where"));
  72.  
  73.     // Test the substring operations
  74.     line(3, 5) = "HELLO";
  75.     printf ("Line is '%s'\n", line.AsPtr());
  76.     line (8,2) += "JUNK";
  77.  
  78.     line (39,3) += "Some tail-end junk";
  79.     line (22,0) = " Inserted text "; // Insert before position 22
  80.     line (22,9) = ""; // Remove the word " Inserted "
  81.     
  82.     printf ("Count %ld\n", line (5, 20).ToLower ());
  83.     printf ("Count %ld\n", line(10,7).ToUpper ());
  84.     CL_String flds[30];
  85.     s = line.Field (0); printf ("Field 0: '%s'\n", s.AsPtr());
  86.     s = line.Field (1); printf ("Field 1: '%s'\n", s.AsPtr());
  87.     s = line.Field (2); printf ("Field 2: '%s'\n", s.AsPtr());
  88.     s = line.Field (5); printf ("Field 5: '%s'\n", s.AsPtr());
  89.     s = line.Field (7); printf ("Field 7: '%s'\n", s.AsPtr());
  90.     CL_StringSplitter split (line);
  91.     char* seps = " ";
  92.     for (split.Reset(); s = split.Next(seps), s.Length() > 0;) {
  93.         printf ("s: '%s'\n", (const char*) s);
  94.         seps = "w";
  95.     }
  96.  
  97.     printf ("Testing the split into a string sequence:\nString: '%s'\n",
  98.             line.AsPtr());
  99.     CL_Sequence<CL_String> seq = line.Split();
  100.     for (short i0 = 0; i0 < seq.Size(); i0++)
  101.         printf ("Field %d: '%s'\n", i0, seq[i0].AsPtr());
  102.     printf ("------------------\n");
  103.     
  104.     line = "Another string";
  105.     s = line.Field (2);
  106.  
  107.     s = CL_String ("The gcd of ") + 7 + " and " + CL_String (19L)
  108.         + " is ";
  109.     s = s + 1;
  110.     printf ("%s\n", (const char*) s);
  111.     
  112.     CL_Integer i1(5938), i2(9), i3(5), i4 (93);
  113.     printf ("First: %d\n", i1.CompareWith (i4, CL_Object::OP_PREFIX));
  114.     printf ("Second: %d\n", i1.CompareWith (i2, CL_Object::OP_EQUAL));
  115.     printf ("Third: %d\n", i1.CompareWith (i4, CL_Object::OP_CONTAINS));
  116.     printf ("Fourth: %d\n", i1.CompareWith (i3, CL_Object::OP_PREFIX));
  117. }
  118.