Avatar of Skale
Skale
 asked on

How to get last lower case characters as a string from string in C#

I have a string with like below structures and i'd like to show what i want to get at the end.

FRCABisch --> isch
FrCABisch --> isch
YTkumoty --> kumoty


Any help would be grateful.

Thank you.
C#

Avatar of undefined
Last Comment
Skale

8/22/2022 - Mon
Gustav Brock

This wil do:

string input = "FrCABisch";
string trail = String.Empty;

foreach (var character in input)
{
	string check = character.ToString();
	if (character.ToString().Equals(character.ToString().ToUpper()))
	{
		trail = string.Empty;
	}
	else
	{
		trail += character;
	}
}

Console.WriteLine(trail);

// trail -> isch

Open in new window

Eduard Ghergu

Hi,

What're the criteria for getting these specific substrings?
gr8gonzo

I would just do a regular expression:
string lastLowercaseChars = String.Empty;
var re = new Regex("[a-z]+$");
var match = re.Match("FrCABisch");
if(match.Success)
{
  lastLowercaseChars = match.Value;
}

// lastLowercaseChars has your final result

Open in new window


Gustav's method should work, too, but is very inefficient (building and truncating strings multiple times, many character to string conversions). It could be improved greatly by making one pass through the string in reverse order to find the last position of an uppercase character and then do a substring based on that result. The pseudo-code:

int posLastUpper = 0:
for(int i = input.Length - 1; i >= 0; i--)
{
  char c = input[i];
  if(c.IsUpper())
  {
    posLastUpper = i;
    break;
  }
}
string lastLowercaseChars = input.Substring(posLastUpper);

Open in new window


Might require some tweaking (e.g. if posLastUpper is 0 then just return the original string) but that idea should work a little more efficiently.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Eduard Ghergu

Hi,
Sorry, I was not paying attention to the question title.
RegEx is the easiest way, indeed. Some sample code:
List<string> list = new List<string>();
   list.Add("FRCABisch");
   list.Add("FrCABisch");
   list.Add("YTkumoty");

   string pattern = @"([a-z][^A-Z])\w+";
   Regex regex = new Regex(pattern);
   List<string> result = new List<string>();

   foreach (string val in list)
   {
       Match match = regex.Match(val);
       result.Add(match.Value);
   }
   
   Console.Write(result);
Skale

ASKER
Hi Eduard and gr8gonzo,

It works great for examples and just realized it's not work if such a case: ptrain.FrCABisch is it possible start reading from right to left ?
Fernando Soto

Hi Hakan;

The following code snippet should do what you want.
var pattern = "^.+?([a-z]+)$";
List<string> input = new List<string>() {"FRCABisch", "FrCABisch", "YTkumoty"};

foreach (string str in input)
{
    Console.WriteLine("Input string = {0} and the ending is = {1}", str, Regex.Match(str, pattern).Groups[1].Value);   
}

Open in new window

The results will be
Input string = FRCABisch and the ending is = isch
Input string = FrCABisch and the ending is = isch
Input string = YTkumoty and the ending is = kumoty

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
gr8gonzo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Skale

ASKER
Hi gr8gonzo,

You're right working extremely good. Thank you.