How to use C# string EndsWith

Within the C# String Class, the EndsWith method assumes a significant role by providing a mechanism to verify whether a parameter string concludes with the specified string. This method enables developers to conduct precise and reliable checks to determine if a string's concluding characters match a specified string.

C# string EndsWith()

When the EndsWith method is invoked, it performs a meticulous examination of the concluding portion of the parameter string, scrutinizing if it aligns with the specified string. By evaluating this alignment, the method determines whether the parameter string indeed concludes with the specified string, yielding a true result if they match, and false if they do not align.

bool string.EndsWith(string suffix)
  1. suffix - The passing String for it EndsWith.
  1. Boolean - Yes/No.

If the String EndsWith the Parameter String it returns True

If the String doesn't EndsWith the Parameter String it return False

For ex : "This is a Test".EndsWith("Test") returns True

"This is a Test".EndsWith("is") returns False

  1. System.ArgumentNullException : If the argument is null

The EndsWith method empowers developers to conduct targeted assessments of string endings, enabling them to implement conditional logic and make informed decisions based on the presence or absence of specific concluding strings. This functionality proves particularly valuable when working with text-based data and when the outcome of an operation depends on the alignment of concluding characters.

Full Source C#
using System; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string str = null; str = "VB.NET TOP 10 BOOKS"; if (str.EndsWith("BOOKS") == true) { MessageBox.Show("The String EndsWith 'BOOKS' "); } else { MessageBox.Show("The String does not EndsWith 'BOOKS'"); } } } }

When you execute the C# program you will get a message box like "The String EndsWith 'BOOKS' "


The EndsWith method in the C# String Class provides developers with a powerful tool to determine if a parameter string's ending aligns with a specified string. This functionality enables precise evaluations, enhancing the ability to make informed decisions and implement conditional logic based on string endings within the context of C# programming.