
- •maranGraphics
- •CREDITS
- •ACKNOWLEDGMENTS
- •ABOUT THE AUTHORS
- •AUTHORS’ ACKNOWLEDGMENTS
- •TABLE OF CONTENTS
- •HOW TO USE THIS BOOK
- •INTRODUCTION TO C#
- •START VISUAL STUDIO .NET
- •OPEN A NEW C# PROJECT
- •OPEN A C# WEB PROJECT
- •SET JSCRIPT .NET AS THE DEFAULT SCRIPT LANGUAGE
- •EXPLORE THE CLASS VIEW WINDOW
- •VIEW THE CONTENTS WINDOW
- •GET HELP USING THE INDEX WINDOW
- •SEARCH FOR HELP
- •ADD COMPONENTS FROM THE TOOLBOX
- •ADD A TASK TO THE TASK LIST
- •CHANGE FORM PROPERTIES IN THE PROPERTIES WINDOW
- •ADD A CUSTOM TOOLBAR
- •DELETE A TOOLBAR
- •CHANGE THE VISUAL STUDIO ENVIRONMENT
- •MANAGE OPEN WINDOWS
- •OPEN A PROJECT
- •VIEW THE MAIN METHOD
- •COMBINE PROGRAM TYPES
- •ADD REFERENCE TYPES
- •ADD OPERATORS
- •INSERT ATTRIBUTES
- •ENTER CLASSES
- •ADD COMMENTS TO CODE
- •WRITE YOUR FIRST PROGRAM
- •ENTER XML DOCUMENTATION
- •ACCESS DOCUMENTATION
- •LOG A BUG REPORT
- •VIEW INFORMATION ABOUT C# BUILDING BLOCKS
- •PROGRAM CLASSES
- •ADD A CLASS
- •EMPLOY CLASS INHERITANCE
- •PROGRAM INSTANCE CONSTRUCTORS
- •INSERT DESTRUCTORS
- •PROGRAM STRUCTS
- •DISPLAY HEAP AND STACK INFORMATION
- •FIND TYPE INFORMATION
- •PROGRAM CONSTANT EXPRESSIONS
- •SPECIFY VALUE TYPES
- •PROGRAM NUMERIC TYPES
- •PROGRAM THE BOOLEAN TYPE
- •DECLARE REFERENCE TYPES
- •ENTER REFERENCE TYPE DECLARATIONS
- •CONVERT VALUE TYPES TO REFERENCE TYPES
- •PROGRAM POINTER TYPES
- •INSERT THE VOID TYPE
- •ADD INTERFACE PROPERTIES
- •ADD AN INTERFACE INDEX
- •VIEW INFORMATION ABOUT METHODS
- •ADD A METHOD
- •ADD STATIC METHODS
- •INCLUDE NON-STATIC METHODS
- •ENTER DELEGATES
- •PROGRAM EVENTS
- •ADD AN EVENT-HANDLING METHOD
- •VIEW INFORMATION ABOUT ARRAYS
- •ENTER SINGLE-DIMENSIONAL ARRAYS
- •ADD MULTIDIMENSIONAL ARRAYS
- •PROGRAM ARRAY-OF-ARRAYS
- •ITERATE THROUGH ARRAY ELEMENTS
- •SORT ARRAYS
- •SEARCH ARRAYS
- •IMPLEMENT A COLLECTIONS CLASS
- •PROGRAM STRUCTS
- •ADD AN INDEXER
- •INCLUDE ENUMERATIONS
- •CREATE STRING LITERALS AND VARIABLES
- •ASSIGN VALUES TO STRINGS
- •CONCATENATE STRINGS
- •COMPARE STRINGS
- •SEARCH FOR SUBSTRINGS
- •REPLACE CHARACTERS
- •EXTRACT SUBSTRINGS
- •CHANGE THE CHARACTER CASE
- •TRIM SPACES
- •REMOVE CHARACTERS
- •SPLIT A STRING
- •JOIN STRINGS
- •PAD STRINGS
- •VIEW INFORMATION ABOUT PROPERTIES
- •COMPARE PROPERTIES AND INDEXERS
- •PROGRAM PROPERTY ACCESSORS
- •DECLARE ABSTRACT PROPERTIES
- •INCLUDE PROPERTIES ON INTERFACES
- •VIEW INFORMATION ABOUT WINDOWS FORMS
- •ADD A WINDOWS FORM IN THE WINDOWS FORM DESIGNER
- •SET THE FORM TYPE
- •CHOOSE THE STARTUP WINDOWS FORM
- •CREATE A MODAL FORM
- •LAYOUT A FORM
- •SET A FORM LOCATION
- •CHANGE FORM PROPERTIES
- •CREATE A TRANSPARENT FORM
- •AN INTRODUCTION TO WEB FORMS AND CONTROLS
- •CREATE AN ASP.NET WEB SITE
- •CREATE A WEB FORM
- •ADD SERVER CONTROLS TO A WEB FORM
- •READ AND CHANGE PROPERTIES FROM OBJECTS ON A WEB FORM
- •USING SERVER-SIDE COMPONENTS ON WEB FORMS
- •INTRODUCING DATA ACCESS WITH ADO.NET
- •DISPLAY DATA WITH THE DATAGRID CONTROL
- •CONFIGURE THE DATAGRID CONTROL
- •INSERT DATA INTO A SQL DATABASE
- •UPDATE DATA FROM A SQL DATABASE
- •DELETE DATA FROM A SQL DATABASE
- •EXECUTE A STORED PROCEDURE IN A SQL DATABASE
- •READ XML FROM A FILE
- •SAVE XML TO A FILE
- •QUERY XML WITH XPATH
- •APPLY XSL TO XML
- •INTRODUCTION TO DISTRIBUTED APPLICATIONS
- •CREATE AN APPLICATION WITH PRIVATE ASSEMBLIES
- •CREATE AN APPLICATION WITH SHARED ASSEMBLIES
- •VERSION A SHARED ASSEMBLY
- •CONFIGURE A CLIENT FOR A VERSIONED ASSEMBLY
- •CREATE A WEB SERVICE
- •USING A WEB SERVICE
- •INTRODUCTION TO EXCEPTION HANDLING
- •THROWING AN EXCEPTION
- •HANDLING EXCEPTIONS WITH THE CATCH BLOCK
- •USING THE FINALLY BLOCK
- •WRITE ERRORS TO THE APPLICATION LOG
- •BASIC EXAMPLES
- •WHAT’S ON THE CD-ROM
- •USING THE E-VERSION OF THIS BOOK
- •INDEX
- •Symbols & Numbers

C#
SPLIT A STRING
Splitting strings into multiple strings is useful when you are trying to manually parse a large string. The .NET String class can divide strings into a set of substrings
with the Split method.
Ideally when it comes to splitting strings, your string contains a subset of strings that are separated by a common character or escape sequence used as a delimiter between each of the substrings. If you have a common character that is used as a delimiter, you can use the String.Split method to create a string array that contains each logical
The String.Split method takes in an array of Unicode characters that delimit the substrings, and a string array is returned. Optionally, you can provide a second parameter, count, that limits the number of substrings to be added to the resulting string array. If you provide the count parameter you will get the last part of the string in the last element of the array, including the delimiter(s). Also, you need to make sure that the count parameter is positive. If you enter a negative number, the method returns an ArgumentOutofRange exception. Lastly, if you provide a zero for count, you will get an array with no elements.
SPLIT A STRING
⁄ Create a new console application and open the
Class1.cs file.
¤ Rename the namespace to
StringSample.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
‹ Rename the class name to ˇ Add the Main function. |
|
|
|
|||||
Á Create a string variable |
|||||||||
|
|||||||||
Split. |
that is initialized with a name |
||||||||
› Save the file. |
of an image file. |
||||||||
|
|
|
178

WORKING WITH STRINGS 8
If you format files with a standard naming convention, you can split the filename into logical substrings that can be used later in your programming logic.
TYPE THIS (INTO YOUR CLASS):
static void Main() {
string sFileName = "hri_disney_jan2001_001.jpg"; string[] sFileParts = new string[4];
char [] cDelim = new char[1] {char.Parse("_")}; sFileParts = sFileName.Split(cDelim,4);
string sPhotoType; string sPhotoEvent = sFileParts[1]; string sPhotoDate = sFileParts[2];
string sPhotoIndex = sFileParts[3].Remove(3,4);
switch (sFileParts[0]) { case "hri" :
sPhotoType = "high resolution image"; break; case "tni" :
sPhotoType = "thumbnail image"; break; default :
sPhotoType = "unknown image type"; break;
}
Console.WriteLine("The " + sPhotoType + " selected was "
+"index " + sPhotoIndex + " of pictures at "
+sPhotoEvent + " which was taken "
+sPhotoDate + ".");
}
RESULT:
C:\>csc SplitString_ai.cs
C:\> SplitString_ai.exe
The high resolution image selected was index 001 of pictures at disney which was taken jan2001.
C:\>
‡ Create size 4.
° Add to split four
underscore the

C#
JOIN STRINGS
The String class provides methods for joining several strings and merging them into one continuous string. The String.Join method lets you join the strings
with separators you specify between each string.
Joining strings together is common when interacting with relational databases. For example, when you build a string that contains the full address for a customer, you should not store this entire string in the database. For more efficient use of your database, you will normalize the storage of the address into separate fields and/or tables. Because the address will be in separate fields, when you pull the data from the database and display it on a user interface, you will need to join the strings together.
You can implement the String.Join in two ways. The simplest implementation requires two parameters. The first parameter is a string that designates the separator used between each substring. The second parameter is an array of strings. Most likely, you will have a String array before calling this method, but you can give the array list nested in the parameter (for example, new string[]{"a","b","c"} ).
The other implementation includes the same parameters as the other with two additional parameters. These parameters are startIndex, which sets the first array element used in the join, and count, which limits the number of elements used in the join.
JOIN STRINGS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
‹ Rename the class name to ˇ Add the Main function. |
Á Create a string array of |
||||||||
|
|
|
|
|
||||||||||
|
|
|
|
the |
Join. |
size 6 and initialize with six |
||||||||
|
|
|
|
|
|
› Save the file. |
image filenames. |
|||||||
|
|
|
|
namespace to |
|
|
|
|||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
|
|
|
|
|
|

WORKING WITH STRINGS 8
You can pull data from enumerations to populate a string array and with that array you can join the members into one string.
TYPE THIS:
using System;
using System.Globalization;
namespace StringSample
{
class WeekDays
{
static void Main()
{
string [] sDaysOfTheWeek = new string[7]; DateTimeFormatInfo dtfInfo =
new DateTimeFormatInfo(); sDaysOfTheWeek = dtfInfo.DayNames; string sWeekDays = String.Join
(", ",sDaysOfTheWeek,1,5 ); Console.WriteLine
("The week days are: " + sWeekDays);
}
}
}
RESULT:
C:\>csc JoinStrings_ai.cs
C:\> JoinStrings_ai.exe
The week days are: Monday, Tuesday, Wednesday, Thursday, Friday
C:\>
‡ Create a string variable and initialize the variable using the Join function to join the elements of the string array together.
° Format a message and write the message to the console.
· Set a debug stop.
‚ Press F5 to save, build, and run the console application.
■ A message appears that shows all of the filenames.
181