C# Sharp Exercises: Compare two substrings
C# Sharp String: Exercise-21 with Solution
Write a C# Sharp program to compare (less than, greater than, equal to ) two substrings.
Sample Solution:-
C# Sharp Code:
// Example for String.Compare(String, Int32, String, Int32, Int32)
using System;
class Example21
{
public static void Main() {
// 01234567
String str1 = "computer";
String str2 = "system";
String str;
int result;
Console.WriteLine();
Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
result = String.Compare(str1, 2, str2, 0, 2);
str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1);
Console.Write("{0} ", str);
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(0, 2), str2);
}
}
Sample Output:
str1 = 'computer', str2 = 'system' Substring 'mp' in 'computer' is less than substring 'sy' in 'system'.
Flowchart:
C# Sharp Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a program in C# Sharp to insert a substring before the first occurrence of a string.
Next: Write a C# Sharp program to compare two substrings that only differ in case. The first comparison ignores case and the second comparison considers case.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework