Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharp - Your Visual Blueprint For Building .NET Applications (2002) [eng].pdf
Скачиваний:
37
Добавлен:
16.08.2013
Размер:
9.71 Mб
Скачать

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

Соседние файлы в предмете Программирование на C++