Link to home
Start Free TrialLog in
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.
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

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

Hi,

What're the criteria for getting these specific substrings?
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.
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);
Avatar of Skale
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 ?
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

ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Skale

ASKER

Hi gr8gonzo,

You're right working extremely good. Thank you.