String Methods - I

Methods Called with the String Class

string.Compare(expression1, expression2)

Returns a result of type int, comparing two texts given as parameters. The result is 0 if the two expressions are equal, -1 if the first expression is small, and 1 if the first expression is large. 

Comparison is made in alphabetical order of letters. For example, the letter b is considered larger than the letter a.

Sample:

string text1 = textBox1.Text;
string text2 = textBox2.Text;

int compare = string.Compare(text1, text2);

if (compare==0) label1.Text="Texts Equal";
else if (compare==1) label1.Text="First Text Large";
else label1.Text="Second Text Uppercase";

A variable or expression can be given as a parameter:

int compare = string.Compare(text1, "Victory");

int compare = string.Compare("Victory", "Remembrance");

string.Concat(parameters)

Returns the result by combining the expressions given as parameters. It can take two or more parameters.

Sample:

string text1 = textBox1.Text;
string text2 = textBox2.Text;
 
label1.Text = string.Concat(text1, " ", text2," what's up");

Different types of data can also be combined with this method:

string text1 = textBox1.Text;
int number1 = 55;
bool case = true;
 
label1.Text = string.Concat(text1, " ", number1, status, 68);

string.Format() Method

It allows to print expressions of text, number or date type according to a certain order.There are quite a few uses. A few methods are mentioned below as examples.

Example: To print an expression with at least 10 characters, see the example below. The required number of spaces will be placed in front of the expression, so that it will take up at least 10 characters in total. If the length of the text is more than 10 characters, nothing will be done.

string text="z";
                        
for (int j = 1; j <= 10; j++)
{
label1.Text += string.Format("{0,10}",text)+"\n";
text = text + "z";
}

If -10 was entered instead of 10, spaces would be added to the right of the text.

Example: If we want to show a number at least 5 digits long by prefixing it with 0's:

int number = 55;
label1.Text = string.Format( "{ 0, 5:00000 }", number ) ;  

The result will be "00055".

Example: To round the digits of a number to 5 and the total number of characters to 8:

int number = 55;
label1.Text = string.Format( "{ 0, 8:00000 }", number ) ;

The result will be " 00055". The number of characters has been completed to 8 by placing 3 spaces on the left side.

Example: To group the digits of a phone number and leave a space between them:

long telNo = 2127271234;
label1.Text = string.Format( "{ 0:### ### ## ## }", telNo) ;

The result will be "212 727 12 34".

Example: To make a decimal number appear at least 3 digits with zeros added to the left of the whole part, and at least 4 digits with zeros added to the right of the decimal part (Note: If the decimal part is longer than 4 digits, it will be rounded):

double number = 12.12;
 
label1.Text = string.Format("{0:000.000}", number);
 
The result will be "012.120".
 

Method string.IsNullOrEmpty(variable name)

Note: First of all, examine the subject about null and empty variables.

It checks whether the string variable given as a parameter is empty. The result will be true if the variable is null or empty, and false if it is full.

string a="aa";
 
if (string.IsNullOrEmpty(a)) label1.Text = "variable is empty";
else label1.Text = "variable full";

Check out the examples below.

c# ready-made methods and their explanations, c# ready-made method examples, string methods, visual c# string Format, Compare method, IsNullOrEmpty, insertion into the text

EXERCISES

Checking array items with IsNullOrEmpty method

string[] numbers = new string[5];

numbers[0] = "534";
numbers[2] = "115";
numbers[3] = "";
A 5-element sequence of numbers is defined above, and then some of its elements are assigned a value. Elements 0 and 2 were assigned a value, element 3 was determined as empty , elements 1 and 4 were left as null , that is, no assignment was made.
 
We can use the IsNullOrEmpty method to set "-" for empty elements while printing all the elements of the array into the label :

foreach (string in numbers)

{

     if ( string.IsNullOrEmpty(i) ) label1.Text += "-\n";

     else label1.Text += i + "\n";

}

 



COMMENTS




Read 610 times.

Online Users: 316



string-methods-called-with-string-class