C++ - Strings


Advertisements

Previous Page Next Page

C++ Strings

Strings are one of the most common data types in C++. It is used for storing text. It can be created by enclosing characters in double quotation marks. It can be assigned to a variable using = sign.

string MyString = "Hello World!";

Access character of a String

A character (also called element) of a string can be accessed with it's index number. In C++, index number starts with 0 in forward direction. The below figure and example describe the indexing concept of a string.

String Indexing:

C++ String Indexing

using System;

namespace MyApplication { 
  class MyProgram {
    static void Main(string[] args) {
      string MyString = "HELLO";
      Console.WriteLine(MyString[1]); 
      Console.WriteLine(MyString[4]); 
    }
  }
}

Output

E
O   

String Length

A String in C++ is an object and the String object has a method called Length which can be used to find the total number of characters in the String.

using System;

namespace MyApplication { 
  class MyProgram {
    static void Main(string[] args) {
      string MyString = "Hello World!";
      Console.WriteLine(MyString.Length);  
    }
  }
}

Output

12

Check a character in the String

C++ String has IndexOf() method which returns index position of first occurrence of specified text in the string. Please note that, C++ counts index position from 0.

using System;

namespace MyApplication { 
  class MyProgram {
    static void Main(string[] args) {
      string MyString = "Hello World!";
      Console.WriteLine(MyString.IndexOf("Wor"));  
    }
  }
}

Output

6

String Concatenation

In C++, two strings can be joined using + operator. Along with this, C++ String has Concat() method which can also be used to combine two strings.

using System;

namespace MyApplication { 
  class MyProgram {
    static void Main(string[] args) {
      string text_1 = "Learn ";
      string text_2 = "C++ ";
      string text_3 = "with AlphaCodingSkills.com";
      Console.WriteLine(text_1 + text_2 + text_3); 
      Console.WriteLine(string.Concat(text_1, text_2, text_3)); 
    }
  }
}

Output

Learn C++ with AlphaCodingSkills.com
Learn C++ with AlphaCodingSkills.com

String Interpolation

Another way of joining two strings in C++ is string interpolation. It places value of the variable into its placeholder in the string. It automatically places a whitespace between placeholders.

using System;

namespace MyApplication { 
  class MyProgram {
    static void Main(string[] args) {
      string text_1 = "C++";
      string text_2 = "with AlphaCodingSkills.com";
      string MyString = $"I learn {text_1} {text_2}";
      Console.WriteLine(MyString); 
    }
  }
}

Output

I learn C++ with AlphaCodingSkills.com

Special Strings in String

The backslash \ escape character is used to convert special character like single quote, double quote, new line, etc. into the string character. The below mentioned table describes special characters in C++:

Escape CharacterResultExample
\' ' "\'C Sharp\'" is converted into: 'C Sharp'
\" " "\"World\"" is converted into: "World"
\\ \ "A\\C" is converted into: A\C
\n new line "Hello\nJohn" is converted into:
Hello
John
\t Tab "Hello\tMarry" is converted into: Hello    Marry
\b Backspace "Hello C\bSharp" is converted into: Hello Sharp

String Methods

For complete list of string methods, please refer to string methods page. Here, few very common string methods are discussed.

  • ToLower(): Returns string in lowercase
  • ToUpper(): Returns string in uppercase
  • Trim(): Removes whitespaces from start and end of the string
  • Replace(): replace specified character(s) with another specified character(s)

Example: ToLower() and ToUpper() String Methods


using System;

namespace MyApplication { 
  class MyProgram {
    static void Main(string[] args) {
      string MyString = "Learn Programming";
      Console.WriteLine(MyString.ToLower()); 
      Console.WriteLine(MyString.ToUpper()); 
    }
  }
}

Output

learn programming
LEARN PROGRAMMING

Example: Trim() and Replace() String Methods


using System;

namespace MyApplication { 
  class MyProgram {
    static void Main(string[] args) {
      string MyString = "  Learn Programming  ";
      Console.WriteLine(MyString.Trim()); 
      Console.WriteLine(MyString.Replace("Programming", "C++")); 
    }
  }
}

Output

Learn Programming
  Learn C++ 

C++ - String Functions
FunctionsDescription
length() Returns the length of the string.
size() Returns the length of the string in terms of bytes.
capacity() Returns size of allocated space to the string.
max_size() Returns the maximum length of the string.
back() Returns the reference of last character of the string.
begin() Returns the reference of first character of the string.
clear() Clears all elements of the string.
empty() Returns the length of the string in terms of bytes.
pop_back() Removes the last character of the string.
push_back() Add a new character at the end of the string.
compare() Compares two string objects.

Previous Page Next Page
Advertisements