Strings – C# Tutorial | A Definitive Programming Guide
Last updated on 16th Aug 2022, Blog, Tutorials
C# first character of a string
String is a data structure that is the same to the array data structure in terms of representation, and implementation but in strings.Save a sequence of characters (char data type) which contains alphabets, numbers, spaces and other special characters.
first n characters substring from the string in C# :
String characters are to be zero-indexed. A string’s initial character is placed at position 0 at the beginning. Get a substring of the first 12 characters from a string. Can use the Substring method and pass it starting index 0 and length of the substring 12 to get the first 12 characters substring from a string.
Capitalize the first character of string in C#:
String is immutable in C#. That means once a string object is created, it cannot be changed in memory. If you capitalize the first character of a string, you have to generate a new string.The String class in C# not provide anything built-in to capitalize the first character of a string. Can write the custom routine for this easy task. There are various ways to achieve that, as below:
- using System;
- using System.Linq;
- public static class Extensions
- {
- public static string FirstCharToUpper(this string str)
- {
- if (String.IsNullOrEmpty(str)) {
- throw new ArgumentException(“Input string is empty!”);
- }
- return str.First().ToString().ToUpper() + str.Substring(1);
- }
- }
- public class Example
- {
- public static void Main()
- {
- string str = “tech”;
- string filtered = str.FirstCharToUpper();
- Console.WriteLine(filtered);
- }
- }
C# | String.IndexOf( ) Method
In C#, IndexOf() method is a string method. This method is used to find the zero-based index of the first occurrence of a specified character or string within the current instance of a string.The method returns -1 if the character or string is not found. This method can be overloaded by passing various parameters to it.
- String.IndexOf(char x)
- String.IndexOf(char x, int start1)
- String.IndexOf(char x, int start1, int start2)
- String.IndexOf(string s1)
- String.IndexOf(string s1, int start1)
- String.IndexOf(string s1, int start1, int start2)
- String.IndexOf(string s1, int start1, int start2, StringComparison cType)
- String.IndexOf(string s1, int start1, StringComparison cType)
- String.IndexOf(string s1, StringComparison cType)
String.IndexOf(char x) method:
Syntax:
public int IndexOf(char x)
Parameters: This method takes the parameter char x of type System.Char which denotes the character to be searched
.Return Type: The return type of this method is a System.Int32.
String.IndexOf(char x, int start1) method:
This method returns the zero-based index of a first occurrence of the particular character within the string. The searching of that character will start from a particular position and if not found it returns -1.
Syntax:
public int IndexOf(char x, int start1)
Parameters: This method takes 2 parameters i.e char x of the type System.Char which specify the character to be searched and start1 of type System.Int32 which denotes the starting position in the form of integer value from where the searching is to be started.
Return Type: The return type of this method is a System.Int32.
Exception: This method gives an ArgumentOutOfRangeException if the start1 is less than 0 (zero) or greater than the length of a string.
String.IndexOf(char x, int start1, int start2) method:
This method returns the zero-based index of a first occurrence of the specified character within the string. The searching of that character will start from a particular position start1 till specified position i.e start2 and if not found it returns -1.
Syntax:
public int IndexOf(char x, int start1, int start2)
Parameters: This method takes 3 parameters i.e char x of type System.Char which specifies the character to be searched, start1 of type System.Int32 which specify the starting position in the form of integer value from where searching is to be started and start2 of type System.Int32 which denotes the ending position where searching is to be stopped.
Return Type:The return type of this method is a System.Int32.
Exception: This method can give an ArgumentOutOfRangeException if the start1 or start2 is negative or start1 is greater than the length of a current string or start2 is greater than the length of current string minus start1.
String.IndexOf(string s1) method:
This method returns a zero-based index of the first occurrence of the specified substring within the string.In case no such string is found then it returns -1 same as in case of characters.
Syntax:
public int IndexOf(string s1)
Parameters: This method takes a parameter s1 of type System.String which specifies the substring to search.
Return Type: The return type of method is System.Int32. The zero-based index position of s1 if that string is found, or -1 if it is not.If s1 is String.Empty, return value be 0.
Exception: This method can give a ArgumentNullException if the s1 is null.
Remove First Character From String in C#:
The String.Remove(x, y) method in C# removes a string value of a particular length and starts index from the original string.It gives back a new string after removing the characters from the original string that are at index x and have length y. Can pass 0 as starting index and 1 as the length to the String.Remove() method to remove the first character from the string.
using System;
- namespace remove_first_character
- {
- class Program
- {
- static void Main(string[] args)
- {
- string s1 = “this is something”;
- s1 = s1.Remove(0, 1);
- Console.WriteLine(s1);
- }
- }
- }
Remove First Character From String with the string.substring() method in c#:
- Can also achieve the same goal by using the String.Substring() method.
- The String.Substring(x) method gets a smaller string from the original string ., starts from the index x in C#.
- Can pass 1 as the starting index to remove the first character from the string.
using System;
- namespace remove_first_character
- {
- class Program
- {
- static void Main(string[] args)
- {
- string s1 = “this is something”;
- s1 = s1.Substring(1);
- Console.WriteLine(s1);
- }
- }
- }
Applications of String:
Plagiarism Checker:
- Strings can be used to find Plagiarism in codes, and contents in a very small amount of time using string matching algorithms.
- Using this the computer could simply tell us the percentage of code, and text written by any two users matches by how much percent.
Encoding/Decoding(Cipher Text Generation):
- Strings can be used for encoding and decoding for the safe transfer of data from sender to receiver to make sure no one in the way of transmission gets to read the data as they could perform both active and passive attacks.
- Text transfer as a message gets ciphered at the sender’s end and decoded at a receiver’s end.
Information Retrieval:
- String applications help to retrieve information from unknown data sources along with the help of string matching/retrieval module helps us to retrieve more important information.
Improved Filters For Approximate Suffix-Prefix Overlap issues:
- Strings and its algorithm applications help to provide increased Filters for the Approximate Suffix-Prefix Overlap Problem.
- The approximate suffix-prefix overlap problem is to find all the pairs of strings from a given set such that a prefix of one string is same to a suffix of the other.
Real-Time Application of String:
Spam Detection: Strings can be used to serve as spam detection systems as the concept of string matching algorithm will be applied to here.Spam could cause many financial losses.All the spam filters use the concept of the string matching to identify and discard the spam.
Bioinformatics: Strings used in the field of Bioinformatics. String matching modules can be used to solve issues regarding the genetic sequences and to find the patterns in DNA.
Intrusion Detection System: Strings can be used in intrusion detection systems. Packets that include intrusion related keywords are found by applying string matching algorithms.
Search Engines: Strings can be used in more search engine techniques.Most of the data is available on the internet in the form of textual data. Due to the large amount of uncategorized text data, it becomes really complex to search a particular content.Web search engines organize the data and to categorize the data string matching the algorithms used.
Are you looking training with Right Jobs?
Contact Us- Windows Azure Interview Questions and Answers
- Salesforce Architecture Tutorial
- Wrapper Class in Salesforce Tutorial
- salesforce lightning
Related Articles
Popular Courses
- VM Ware Training
11025 Learners
- Microsoft Dynamics Training
12022 Learners
- Siebel Training
11141 Learners
- What is Dimension Reduction? | Know the techniques
- Difference between Data Lake vs Data Warehouse: A Complete Guide For Beginners with Best Practices
- What is Dimension Reduction? | Know the techniques
- What does the Yield keyword do and How to use Yield in python ? [ OverView ]
- Agile Sprint Planning | Everything You Need to Know