Basics

You can shorten the code required to concatenate a string to itself by using the += assignment operator. The sample below passes arguments into a console application from the command line and builds a single string that puts all the command line parameters into one string.

COMPILE THIS:

using System;

public class ApplyStringConcatenation

public static void Main(String[] args)

String strDynamicString = "";

// Loop through the arguments and concatenate into one string for(int i = 0; i < args.Length; i++)

// Write the result of the concatenated string to the console Console.WriteLine("Your goal list is: " + strDynamicString);

RESULT:

Your goal list is: Run a marathon; Go to top of Empire State Building;

TYPE THIS:

<program name>.exe " Run a marathon" "Go to top of Empire State Building"

File Edit Formal Help

^oncatenateString blic static void MainQ

string strFirst - "set"; string strSecond = "goals";

string strSimpleStatement - strFirst + strSecond;

string strBetterStatement = "You should " + strFirst + " " + strSecond + " monthly";

Console.WriteLine("A simple sample of concatenating two strings:111 +

strSimpleStatement + );

Console.WriteLine("A sample created from concatenating variables with text:1 strBetterStatement + );

Q Concatenate the strings within a statement.

Write the two statements to the console.

, Save as the class name.

Note: See page 35 for more information on saving a file.

Microsoft Uindous 2000 [Uersion 5.00.2195] <C> Copyright 1985 2000 Microsoft Corp.

C:\Docunents and Settings\Administrator>cd C:\CSharp

C:\Gsharp>csc ConcatenateString.es

Microsoft <R> Uisual Ctt Compiler Uersion 7.00.9148 CCLR uersion ul.0.: 6151

Copyright <C> Microsoft Corp 2000. fill rights reserved. C:\Csharp>ConeatenateString.exe

A simple sample of concatenating tuo strings: 'setgoals' A sample created front concatenating variables with text: 'Vou should j et goals monthly'

■ The program displays the message about the concatenated strings.

Was this article helpful?

0 0

Post a comment