0% found this document useful (0 votes)
21 views13 pages

C# String Array Operations Guide

The document contains code examples demonstrating various operations on strings in C#. These include initializing and accessing elements in a string array, sorting a string array, substring operations, string length, concatenation, case conversion, comparisons, containment checks, copying strings, reading strings line by line, using StringBuilder to efficiently build strings, and searching/replacing characters in strings.

Uploaded by

sandeep_nayak_17
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views13 pages

C# String Array Operations Guide

The document contains code examples demonstrating various operations on strings in C#. These include initializing and accessing elements in a string array, sorting a string array, substring operations, string length, concatenation, case conversion, comparisons, containment checks, copying strings, reading strings line by line, using StringBuilder to efficiently build strings, and searching/replacing characters in strings.

Uploaded by

sandeep_nayak_17
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Initialize string array

using System;

class MainClass
{
  public static void Main()
  {
    string[] stringArray = {"Hello", "World"};
    foreach (string myString in stringArray)
    {
      [Link]("myString = " + myString);
    }
  }
}

change element in a string array


using System; 
 
class MainClass {  
  public static void Main() {  
    string[] str = { "This", "is", "a", "test." };  
  
    [Link]("Original array: ");  
    for(int i=0; i < [Link]; i++) 
      [Link](str[i] + " ");  
    [Link]("\n");  
  
    // change a string  
    str[1] = "was";  
    str[3] = "test, too!";  
  
    [Link]("Modified array: "); 
    for(int i=0; i < [Link]; i++) 
      [Link](str[i] + " ");  
  }  
}
Sort a string array
using System;

class MainClass
{
  public static void Main()
  {
    
    string[] stringArray = {"t", "i", "a", "test", "abc123", "abc345"};
    [Link](stringArray);  
    [Link]("Sorted stringArray:");
    for (int i = 0; i < [Link]; i++)
    {
      [Link]("stringArray[" + i + "] = " + stringArray[i]);
    }
  }

SUBSTRING
using System;  
  
class MainClass {  
  public static void Main() {  
    string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
 
    [Link]("str: " + str); 
     
    [Link]("[Link](15): "); 
    string substr = [Link](15); 
    [Link](substr); 
 
  } 
}

using System;  
  
class MainClass {  
  public static void Main() {  
    string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
 
    [Link]("str: " + str); 
     
    [Link]("[Link](0, 15): "); 
    string substr = [Link](0, 15); 
    [Link](substr); 
 
  } 
}
string length
using System;  
  
class MainClass {   
  public static void Main() {   
    string str1 = "ABCDEabcde1234567890";   
  
    [Link]("str1: " + str1); 
 
    [Link]("Length of str1: " + [Link]);   
  }
}

STRING CONCATENATION
using System; 
 
class MainClass { 
  public static void Main() { 
 
    string result = [Link]("This ", "is ", "a ", 
                                  "test ","."); 
 
    [Link]("result: " + result); 
 
  } 
}
LOWERCASE,UPPERCASE
using System;  
  
class MainClass {   
  public static void Main() {   
    string str1 = "ABCDEabcde1234567890";   
    
    string strLow = [Link](); 
    string strUp =  [Link](); 
    [Link]("Lowercase version of str1:\n " +  strLow); 
    [Link]("Uppercase version of str1:\n " +  strUp); 
  }
}

String comparisons:ignore case


 public static void Main()
    {
        bool b1 = "hello" == "hello";      
        [Link](b1);
        bool b2 = "hello" == "hi";         
        [Link](b2);
        bool b3 = "hello".Equals("hello"); 
        [Link](b3);
        bool b4 = "hello".Equals("hi");    
        [Link](b4);
        bool b5 = "HoWdY".Equals("howdy"); 
        [Link](b5);
        bool b6 = "HoWdY".Equals("howdy", [Link]); 
        [Link](b6);
    }
Compare string case sensitively

using System; 
 
class MainClass { 
  public static void Main() { 
    string str1 = "one"; 
    string str2 = "ONE"; 
 
    if([Link](str1, str2, true) == 0) 
      [Link](str1 + " and " + str2 + 
                        " are equal ignoring case."); 
    else 
      [Link](str1 + " and " + str2 + 
                        " are not equal ignoring case."); 
 
  } 
}

 If two string are equal

using System; 
 
class MainClass { 
  public static void Main() { 
    string str1 = "one"; 
    string str2 = "one"; 
 
    if([Link](str1, str2) == 0) 
      [Link](str1 + " and " + str2 + 
                        " are equal."); 
    else 
      [Link](str1 + " and " + str2 + 
                        " are not equal."); 
 
  } 

String Contains a string

using System;  
 
class MainClass {  
  public static void Main() {  
    string str = "abcdefghijk"; 
 
    if([Link]("def"))  
      [Link]("The sequence 'def' was found."); 
 
  }  
}

Copy a string

 Reading from a string one line at a time

using System;

using [Link];

using [Link];

using [Link];

using [Link];

using [Link];

using [Link];

using [Link];

using [Link];

using [Link];

public class MainClass

    public static void Main()

    {
    

    

        string contents = "Test content\r\nHello there";

        using (StringReader reader = new StringReader(contents))

        {

            int lineNo = 0;

            string line;

            while ((line = [Link]()) != null)

            {

                [Link]("Line#{0}: {1}", ++lineNo, line);

            }

        }

    }

Line#1: Test content


Line#2: Hello there

String writer

using System;  

  
class MainClass {   
  public static void Main() {   
    string str1 = "ABCDEabcde1234567890";   
    string str2 = [Link](str1);  
    string str3 = "asdf";   
  
   
    // compare strings 
    if(str1 == str2)   
      [Link]("str1 == str2");   
    else   
      [Link]("str1 != str2");   
   
    if(str1 == str3)   
      [Link]("str1 == str3");   
    else   
      [Link]("str1 != str3");   
  
  }

using System;

using [Link];

using [Link];

using [Link];

using [Link];

using [Link];

using [Link];

using [Link];

using [Link];

using [Link];

public class MainClass

    public static void Main()

    {

    

        StringWriter writer = new StringWriter();

        [Link]("Name: {0}, Age: {1}", "Henry", 32);
        [Link]([Link]());

    }

Name: Henry, Age: 32

String Inserting

using System;  
  
class MainClass {  
  public static void Main() {  
    string str = "This test"; 
 
    [Link]("Original string: " + str); 
     
    // Insert 
    str = [Link](5, "is a "); 
    [Link](str); 
 
  } 
}
Replace string

using System;  
  
class MainClass {  
  public static void Main() {  
    string str = "This test"; 
 
    [Link]("Original string: " + str); 
     
    // Replace string 
    str = [Link]("is", "was"); 
    [Link](str); 
 
  } 
}

Replace characters

using System;  
  
class MainClass {  
  public static void Main() {  
    string str = "This is a test"; 
 
    [Link]("Original string: " + str); 
     
    // Replace characters 
    str = [Link]('a', 'X'); 
    [Link](str); 
 
  } 
}
String removes from start index to end index

using System;  
  
class MainClass {  
  public static void Main() {  
    string str = "This is a test"; 
 
    [Link]("Original string: " + str); 
     
    // Remove 
    str = [Link](4, 5); 
    [Link](str); 
  } 
}

Search a character in a string

using System; 
 
class MainClass { 
  public static void Main() { 
    string str = "abcdefghijk"; 
    int idx; 
 
    [Link]("str: " + str); 
 
    idx = [Link]('h'); 
    [Link]("Index of first 'h': " + idx); 
 
  } 
}
Add formatted string to StringBuilder

using System;
using [Link];

class MainClass
{
    public static void Main()
    {
        StringBuilder sb = new StringBuilder();
        int number = 1;

        [Link]("{0}: {1} ", number++, "another string");

        [Link]("{0}", sb);
    }
}

Append('A ').Append('B ').Append('C... ')

using System;
using [Link];

public class MainClass
{
    static void Main() {
        StringBuilder sb = new StringBuilder();

        [Link]("A ").Append("B ").Append("C... ");

        string built1 = [Link]();

        [Link]("D");

        string built2 = [Link]();
        [Link]( built1 );
        [Link]( built2 );
    }
}

Use the Append() method to append a substring to StringBuilder

using System;
using [Link];

class MainClass
{
  public static void Main()
  {
    StringBuilder myStringBuilder = new StringBuilder();
    
    [Link]("myString");
    
    
    [Link]("Here's another string", 0, 4);

    [Link](myStringBuilder);
  }
}

myStringHere

You might also like