Strings Questions and Answers

1.) What is String?                                                                                                                 
A string is a  combination of sequence of characters.It is one of the built-in types ,provided by .net Framework.

2.) What is Mutable and Immutable string in C#?                                                        
  • Mutable -->means -->change in value
  • Immutable -->means -->No change in value
    More Details..             

3.) How can create Immutable string in C#?                                                                There are some ways to create immutable string in C#.
  • Assigning string Literals
  • Copying from one object to another
  • Concatenating strings
  • The ToString Method
  • Reading from the keyboard
1.)  Assigning string Literals 
   To assign a quoted string of character to a string object ,is known as string literals.
           String s ;           //declaration of string object
            s = " Ram";      // Assigning string literal

2.)  Copying strings
There are two ways to create new copies of existing strings

  • Assignment Method
  • Static copy method
            string s1  = s2 ;     //assigning
             string s1 = string.copy(s2);
3. ) Concatenating strings
    There are two ways to create new strings by concatenating existing strings.
  • Using assignment Method
  • Static concat Method
string s = s1 + s2 ;         // s1 and s2 are existing string
string s = string .concat(s1+s2);
Note:- Both are doing same things but ways are different.
4. ) The ToString() Method
   This ways of creating a string is most commonly used in C#.
        int a = 241;
        string s = a .ToString() ; 
Note:- It converts 241 value to string '241'
5. ) Reading from the keyboard
  You ca easily read a string value from the keyboard and assign it to a string object.
          string s = Console.ReadLine();
Note:-  When you enter the value from the keyboard and press enter Button, then value will be  read and assigned to the string object s.

4.) What is Verbatim strings in C#?                                                                                  
Verbatim strings are those strings ,that start with the Symbol .
string s = @ "\xyz\java\ram.cs";
string s = @ "\\xyz\\java\\ram.cs";

5.) What are the string Method in C#?                                                                             
There are some important string methods in C#.
Methods                                                    Meaning
Copy()                              Create a new string by copying another string.
CopyTo()                        Copies specified number of characters to an array of Unicode character.
Compare()                      It is used for compare two string
CompareTo()                It is used to compare the current instance with another instance.      
Equals()                           It determines if two string are equal.
Insert()                            I Return a new string with a substring insert ed at a specified location.
Remove()                        It is used for delete the characters from the string.
Join()                                It is used to join an array string together.
Replace()                        It is used to replace all instances of a character with a new character.
Split()                               It is used for splitting the string .
Trim()                              It is used to remove white space from the string.
Concat()                           It is used to concatenates two or more strings.

6.) What are the ways to compare the strings in C#?                                                    
There are three  ways to compare the string.
1. ) Compare( ) Method:-
          int a = string . Compare(s1,s2);
Description:-  This string matching method is case sensitive.There are some condition about comparison .

  • If s1 is equal to s2 ,then it returns zero Integer.
  • If s1 is greater than s2 ,then if returns positive Integer(1).
  • If s1 is less than s2 ,then it returns negative Integer (-1).
Note:- If s1 = "ram" and s = "RAM" ,then a will be a value of -1. Because lower case letter has smaller  ASCII value than upper case letter.

2. ) Equal( ) Method:-
   This equals ( ) method also used for comparing to string values.This returns True or false Boolean Value.
      bool a = string Equals (s1,s2);
     bool b = s1. Equals (s2) ;
If s1 & s2 are equal then it return True (1) otherwise false(0)
3. ) The == operator :-
   This is most familiar way of testing the equality of strings by using == operator.
  • If (s1 == s2 )
                   Console.WriteLine("Welcome to my website");                }

  • bool b = ( s1 == s2)
If s1 equal to s2 ,then if statement will be executed.
 If s1 equal s2 then b return True otherwise False.       

7.) What is string Builder in C#?                                                                                       
StringBuilder and string both are act as same in program.But string builder is mutable and string is immutable.
StringBulider  class comes under "System.Text" Namespace.
More Details..

8.) What is array of string in C#?                                                                                  
string [ ] str = new string [10];
Description:- I have created an array str that can  hold ten value.

  • The size of the array ,once created ,can not be changed.
  • ArrayList is a dynamic array.we can easily change(extend) the size of array at Run time.
9.) What is Regular Expression  in C#?                                                                            
A Regular Expression is a tool for searching and manipulating a large text. A  Regular Expression may be applied to a text as following ways.
  • To modify one or more sub strings and return them.
  • To locate sub string and return them.
  • To identify sub strings that begin with or end with a return of characters.
  • To find all the words that begin with  a group of characters.
  • To find all the occurrences of a substring pattern and  more.
10.) What is Namespace used for  Regular Expression  in C#?                              
Using System.Text.Regular Expressions;


Post a Comment

Powered by Blogger.