Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i replace characters in a string using c#?

I have a string

"This is a test string for testing in dotnet4.8 (.net)" 53 chars

The requirement is that if the length of the string is greater than 40 characters
then to replace characters with "" starting from the (.net) moving to the left until the total chars is 40 , so i will end up with

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"This is a test string for testing (.net)"   

and it doesnt matter if the word is cut off , cause there could be scneario where the string is different but they will always end in (.net)

>Solution :

If I understood you right, you want the actual text to be 34 letters long, so when adding your "(.net)" ends up being 40.

so I guess something in the realms of:

public string Shorten(string s)
{
  if(s.Length > 40)
    return s.Substring(0,34) + "(.net)";
  else
    return s;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading