Tuesday 9 November 2021

Remove Last Character from String in C#

 The C# String.Remove method can be used to remove last characters from a string in C#. The String.Remove method in C# creates and returns a new string after removing a number of characters from an existing string.

 
C# String.Remove() method has two overloaded forms: 
  1. Remove(Int32) - Returns a new string in which all the characters in the current instance, beginning at a specified position and continuing through the last position, have been deleted.
  2.  Remove(Int32, Int32) - Returns a new string in which a specified number of characters in the current instance beginning at a specified position have been deleted.
Remove Last Character from a C# String
 
The following code example removes the last character from a string. 
  1. /** Remove sample **/  
  2. string founder = "Mahesh Chand is a founder of C# Corner!";  
  3. // Remove last character from a string  
  4. string founderMinus1 = founder.Remove(founder.Length - 1, 1);  
  5. Console.WriteLine(founderMinus1);  
Remove All Characters After a Character Position
 
The following code example all characters after 25 characters. 
  1. // Remove all characters after first 25 chars  
  2. string first25 = founder.Remove(25);  
  3. Console.WriteLine(first25);  
Remove All Characters In Between A C# String
 
The following code example removes 12 characters after 10th position in a string. 
  1. // Remove characters start at 10th position, next 12 characters  
  2. String newStr = founder.Remove(10, 12);  
  3. Console.WriteLine(newStr);  
  4. int pos = founder.IndexOf("founder");  
  5. if (pos >= 0)  
  6. {  
  7. // String after founder  
  8. string afterFounder = founder.Remove(pos);  
  9. Console.WriteLine(afterFounder);  
  10. // Remove everything before founder but include founder  
  11. string beforeFounder = founder.Remove(0, pos);  
  12. Console.Write(beforeFounder);  
  13. }
Here is a Tutorial on C# String
 
Here is the complete source code of use of the C# String.Replace method. 
  1. using System;  
  2. namespace RemoveStringSample  
  3. {  
  4. class Program  
  5. {  
  6. static void Main(string[] args)  
  7. {  
  8. /** Remove sample **/  
  9. string founder = "Mahesh Chand is a founder of C# Corner!";  
  10. // Remove last character from a string  
  11. string founderMinus1 = founder.Remove(founder.Length - 1, 1);  
  12. Console.WriteLine(founderMinus1);  
  13. // Remove all characters after first 25 chars  
  14. string first25 = founder.Remove(25);  
  15. Console.WriteLine(first25);  
  16. // Remove characters start at 10th position, next 12 characters  
  17. String newStr = founder.Remove(10, 12);  
  18. Console.WriteLine(newStr);  
  19. int pos = founder.IndexOf("founder");  
  20. if (pos >= 0)  
  21. {  
  22. // String after founder  
  23. string afterFounder = founder.Remove(pos);  
  24. Console.WriteLine(afterFounder);  
  25. // Remove everything before founder but include founder  
  26. string beforeFounder = founder.Remove(0, pos);  
  27. Console.Write(beforeFounder);  
  28. }  
  29. Console.ReadKey();  
  30. }  
  31. }  
  32. }

No comments:

Post a Comment

Adding a newline into a string in C# and X++

Below is the sample code we can use for  adding a newline after every occurrence of "@" symbol in the string in C#   using System...