You could use "Split" function to fullfill your need. Try the below code:
string strYourString = "Some string with Special Characters:!@#$%^&*("; // Will give the last 2 charcters *( string strExtracted = strYourString.Substring(strYourString.Length - 2); Response.Write(strExtracted);
0
VulpesRank #55 · 29.2K reputationPosted Feb 1, 2015, 6:49 AM EST
0
If the last two special characters are mixed in with 'normal' characters, then you'll need to do something more elaborate to extract them and leave the rest of the characters untouched.
Try this:
using System;
class Program
{
static void Main()
{
char[] special = ":!@#$%^&*()".ToCharArray(); // or whatever they are
string text = "Quick! brown fox #1 (is jumping over the lazy dog)";
// get indices of last two special characters and extract them from the string
string lastTwoSpecial = "";
int index = text.LastIndexOfAny(special);
if (index > -1)
{
lastTwoSpecial = text[index].ToString();
text = text.Remove(index, 1);
index = text.LastIndexOfAny(special);
if (index > -1)
{
lastTwoSpecial = text[index] + lastTwoSpecial;
text = text.Remove(index, 1);
}
}
Console.WriteLine("The last two special characters are : ");
Console.WriteLine(lastTwoSpecial);
Console.WriteLine("\nThe rest of the string is : ");
Francis SusaimichaelRank #466 · 5.4K reputationPosted Feb 1, 2015, 3:42 AM EST
string strYourString = "Some string with Special Characters:!@#$%^&*(";
// Will give the last 2 charcters *(
string strExtracted = strYourString.Substring(strYourString.Length - 2);
Response.Write(strExtracted);
VulpesRank #55 · 29.2K reputationPosted Feb 1, 2015, 6:49 AM EST
Try this:
The output is:
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts