String Methods - II

Split( ) Method - Split Text into Parts

It is a method that splits a text into parts using a specified character and creates a string type array with these parts.

string text = "Ahmet,Ali,Ayşe,Asli,Arzu";
string[] names = text.Split(','); 
 
foreach (string name in names)
{
     label1.Text += name + "\n";
}

In the above example, the variable named text is split from the characters "," and these parts are transferred to the array named names. Then, with the foreach loop, the array elements are printed on label1.

We can also use a string of characters instead of a single character to split it into parts. In the example below, the characters to be used in splitting are taken into an array and this array is used when calling the split method. In this way, each element in the array of expressions will be used for dividing the text.

string text = "29 October 1923 20:30";
 
char[] expressions = { '/', ".", ' ', ':' }; // text will be split in each of these statements
 
string[] parts = text.Split( expressions ); 

Replace( ) Method - Finding and Replacing Text

It allows to replace a certain expression with another expression in a string.

Example of the use of the Replace method;

label1.Text = a.Replace( "old", "new" );

message = message.Replace("xxx" , "x" );

Substring( ) Method - Getting Part of Text

It is used to get only the desired part of a string expression.

Two parameters of type int are used. It allows us to get a piece of text of the specified length, starting with the character at the specified index number.

Note that index numbers always start from 0.

Substring example;

string a = textBox1.Text;

if ( a.Length >= 5 ) label1.Text = a.Substring(0, 5);
else label1.Text = a;

The a.Substring( 0.5 ) part above allows us to get the first 5 characters of the variable a.

We initially checked the length of variable a using if, because trying to get 5 characters while a is shorter than 5 characters will cause an error.

If the second parameter is not specified when using the substring method, the part from the specified place to the end is taken.

For example, in the example below, the part from the 3rd character of the variable a to the end is taken.

label1.Text = a.Substring(3);

Contains( ) Method - Text Search

It is used to search for a character or expression in a string. Returns True if the searched expression is in a string, and False if it is not.

Example of using Contains:

string a = textBox1.Text;
string b = textBox2.Text;

if (a.Contains(b)) label1.Text = "Search found";
else label1.Text = "Not Found";

In the above example, it is questioned whether variable b is passed in variable a.

EndsWith( ) Method

Checks whether a string expression ends with another expression. Returns true if the text ends with the specified expression, false otherwise.

string a = textBox1.Text;
string b = textBox2.Text;
 
if (a.EndsWith(b)) label1.Text = "Finally there";
else label1.Text = "Not at the end";

In the above example, it is questioned whether variable a starts with variable b.

if (a.EndsWith("t")) label1.Text = "ends with t";
else label1.Text = "does not end with t";

In this example, it is questioned whether variable a ends with the letter "t".

StartsWith( ) Method

A string expression tells us whether it starts with another expression. 

string a = textBox1.Text;
string b = textBox2.Text;
 
if (a.StartsWith(b)) label1.Text = "Has desired start";
else label1.Text = "Does not have the requested beginning";

Example: We can query whether a text starts with the letter "a" and ends with the letter "a", as follows:

if (a.StartsWith("a") && a.EndsWith("a") )
    label1.Text = "Starts and ends with A";
else label1.Text = "Does not start or end with A";

IndexOf( ) Method - Finding Index Number of Searched Text

It allows us to search for another character or expression in a string.

Returns the starting index number if expression is found, unlike the Contains method.

Returns -1 as a result if the expression is not found.

The index number of the first character is 0.

If the searched phrase occurs more than once, it returns the number of the first one it finds.

IndexOf usage example:

string a = textBox1.Text;
string b = textBox2.Text;
 
if ( a.IndexOf(b) != -1)
label1.Text = ( a.IndexOf(b) + 1).ToString( ) + "Found";
else
label1.Text = "Not Found";

The reason why we print the beginning of the expression on label1 above by increasing 1 is because the number of the first character is 0.

If we want the search to be done after a certain place, we can use the IndexOf method as follows.

if ( a.IndexOf ( "z", 3 ) ) ....

In the above usage, we have searched for the letter z starting from the 3rd character of the variable a.

LastIndexOf( ) Method - Finding Index Number of Searched Text

It does the same job as the IndexOf method. However, if the searched phrase occurs more than once, it returns the character number of the last found one.

if ( a.IndexOf(b) != -1)
label1.Text = "found in line " + ( a.LastIndexOf(b)+1 ).ToString();
else
label1.Text = "Not Found";

Insert( ) Method

It allows us to insert another text in a text, starting from the specified place.

Use of the Insert method;

text1.Insert( text2 , indexNo); is in the form. In this type of usage, the text2 variable is inserted into Metin1 starting from the given index number.

label1.Text = a.Insert(3, b);

In the example, the text in variable b is pasted into variable a, starting with the 3rd character.

PadLeft( ) Method

It allows the text to occupy as much space as the desired number of characters. It has two different uses.

label1.Text = a.PadLeft( 5 );

If used as above, it adds a space in front of the text to allow it to take up 5 characters.

If we want it to add another character instead of a space, we can use it like this.

label1.Text = a.PadLeft( 5, '0' );

Here, we have stated that it will take up 5 characters in total and 0 will be placed in front of it to complete the number of characters to 5.

Remove( ) Method

It serves to delete the specified part of a string expression. It can take one or two parameters from the outside.

label1.Text = a.Remove(3);

In the above example, it deletes all characters from the 3rd character of the variable a to the end.

label1.Text = a.Remove(3, 2);

In this example, it deletes 2 characters in total, starting with character 3. So characters 3 and 4 will be deleted.

ToLower( ) and ToUpper( ) Methods

The ToLower method is used to represent the string with which it is called in all lowercase letters. The ToUpper method is used to show all capital letters.

string a = textBox1.Text;
 
label1.Text = a.ToLower( ) + "\n" + a.ToUpper( );

In the example above, the variable a is first printed on the label with lowercase and then uppercase letters.

 

c# string methods, c# get part of text, get the desired part from the text, search for text, understand what the text does not end with, add text inside the text

EXERCISES

There are no examples related to this subject.



COMMENTS




Read 624 times.

Online Users: 774



string-methods