Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Hungry Minds - Visual C# Blueprint.pdf
Скачиваний:
101
Добавлен:
12.02.2016
Размер:
9.71 Mб
Скачать

C#

TRIM SPACES

When you have a string that contains unwanted white space in the beginning or end of a string, you can resolve this with using the trimming

capabilities built into the String class. Visual C# provides three options for creating a copy of your string and trimming the white spaces from them: String.Trim,

String.TrimEnd, and String.TrimStart.

The String.Trim, String.TrimEnd, and

String.TrimStart methods determine what white space is and what is not by using the Char.IsWhiteSpace method. The Char.IsWhiteSpace method indicates whether the specified Unicode character is categorized as white space.

The String.Trim method trims both ends of the string. If only the end or beginning white space characters need to be trimmed, you can use the String.TrimEnd and String.TrimStart methods respectively. These trim methods also have overloaded implementations that allow specified character(s) to be included as nondesirable characters to be trimmed off of the extremes of the string. To include additional characters, you would provide an array of characters in the parameter list of the desired trim method. This character list does not have to be specified in any order. It uses all members of the array as it trims characters off of the string.

TRIM SPACES

console

Rename the class name to

the

TrimSpaces.

namespace to

Save the file.

 

ˇ Add the Main function.

Á Create a string variable and initialize with a welcome message with a space at the beginning and end of the string.

Create a string variable that is initialized with a name.

WORKING WITH STRINGS 8

Trimming strings for more than white space can enable you to strip any other unwanted characters like punctuation.

TYPE THIS:

using System;

namespace StringSample

{

///<summary>

///The Trim can accept an array of unicode chars that

///will be used to trim from either end of the string.

///Note that it does not matter what order the chars

///are set.

///</summary>

class TrimSpaces

{

static void Main()

{

String sGreeting =

" Welcome to My Shared Photo Album! " ;

Console.Write(sGreeting.Trim( char.Parse(" "), char.Parse("!")));

}

}

}

RESULT:

C:\>csc TrimSpaces_ai.cs

C:\> TrimSpaces_ai.exe

Welcome to My Shared Photo Album

C:\>

° Concatenate the message without trimming and output to the console.

· Trim the leading and trailing spaces, concatenate the message, and output to the console.

Trim the trailing spaces, concatenate the message, and output to the console.

Trim the leading spaces, concatenate the message, and output to the console.

± Set a debug stop.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

¡ Press F5 to save, build,

 

 

 

 

 

The message appears

 

and run the console

without a trailing space.

 

application.

The message appears

 

 

 

 

 

 

The message appears with

without a leading space.

 

 

 

leading and trailing spaces.

 

 

 

 

 

The message appears with

 

 

 

 

 

 

 

 

 

 

neither leading nor trailing

 

 

 

 

 

spaces.

 

 

 

 

175

C#

REMOVE CHARACTERS

You can clean up your string of unwanted characters with members of the .NET Framework String class. The String class contains members that can remove

unwanted characters from the extremes of a string or throughout the string.

To trim characters from the extremes (start and end), you can refer to the “Trim Spaces” task in this chapter. To remove characters from anywhere in the string, you can use the String.Remove method. The String.Remove method requires two parameters for execution. The first parameter, startIndex, is an integer that indicates the

starting position for deleting characters. The second parameter, count, is an integer that indicates the number of characters to delete from the startIndex.

When using the Remove method, you will most likely determine the startIndex and count programmatically.

For example, you may know a list of characters that are in data file from a mainframe. You can search for the characters in the string for their position. You can use the

IndexOf and LastIndexOf methods of the String class to see what index position contains that character. This position can be used as the startIndex, and you can give a count of 1.

REMOVE CHARACTERS

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Rename the class name to

 

ˇ Add the Main function.

 

 

 

 

 

 

 

 

 

Á Create a string variable

 

 

 

 

 

 

 

 

 

 

 

 

 

 

RemoveCharacters.

that is initialized with a full

 

 

 

 

 

 

Save the file.

name including the first,

 

 

 

 

 

 

middle, and last name.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

WORKING WITH STRINGS 8

This example takes this section’s example one step further and programitically determines the values for the startIndex and count parameters.

TYPE THIS:

using System;

namespace StringSample

{

class RemoveCharacters

{

static void Main(string[] args)

{

string sUsersFullName = "Austin Joseph Ryan";

string[] sNameParts = sUsersFullName.Split( char.Parse(" "));

if (sNameParts.GetUpperBound(0)==2)

{

string sShortName = sUsersFullName.Remove(sNameParts[0].Length,

sNameParts[1].Length + 1); Console.WriteLine(sShortName);

}

}

}

}

RESULT:

C:\>csc RemoveCharacters_ai.cs

C:\> RemoveCharacters_ai.exe

Austin Ryan

C:\>

Use the Remove function to remove the middle name from the string.

° Set a debug stop.

· Press F5 to save, build, and run the console application.

A message appears with just the first and last name.

177

Соседние файлы в папке c#