Hi i'm new to c#

i'm trying to write a function that i can use over and over again in a number of places.

i want to pass in a couple of parameters
1)the string
2)number of character i want in substring
3)what characters to put after the last white space of substring, this will be either "..." or " " (just nothing).

i see the it like

ltlr.text= function.Formatstring("AString", "200", "...");

what i'm unsure of is how to write the function to pass these paremeters into.

can any one help?

You would need to make this a static member of a class to use it but the basic gist of it is like

string Formatstring(string input,int length,string suffix)
{
     return (input.Substring(0,length)+suffix);
}

You would need to make this a static member of a class to use it but the basic gist of it is like

string Formatstring(string input,int length,string suffix)
{
     return (input.Substring(0,length)+suffix);
}

thanks but wouldn't that cut of a word?

for example take the string "hello how are you" and i want 15 characters but i want it to cut of at the whitespace.

your way would give me "hello how are yo..."

what i would want is

"hello how are..."

so it takes the string, gets the substring, but then finds the last whitespace in that substring and then add the suffice on the end

finds the last whitespace in that substring

Overlooked that bit, apologies.

string Formatstring(string input,int length,string suffix)
{
     string tempstring = input.Substring(0,length);
     int lastindex = tempstring.LastIndexOf(' ');
     return (input.Substring(0,lastindex)+suffix);
}

Here is some code I use to abbreviate text. I wrote it for a recent project, but I haven't really tried to optimize it. To be honest, I haven't thoroughly tested it! (Yet.)

It is similar to what has been posted before, but I add a regular expression to it because, in my case, I don't want to end it on a non-alphanumeric character. (How tacky would it be to end a substring like "have a nice day!...")

At any rate, it's in a static Utilities class so I can use it wherever I need it. The purpose, in this case, is to create teaser text for a longer article. Show the first ~100 characters and then a link to view the rest.

public static string AbbreviateText(string input, int characterLimit)
        {
            return Utilities.AbbreviateText(input, characterLimit, false);
        }

        public static string AbbreviateText(string input, int characterLimit, bool trailingDots)
        {
            string temp = input;

            if (temp.Length > characterLimit)
            {
                temp = temp.Substring(0, characterLimit);
                int length = temp.LastIndexOf(' ');
                if (length <= 0)
                    length = characterLimit;
                temp = temp.Substring(0, length);

                Regex regex = new Regex("[A-Za-z0-9]$");
                while (!regex.IsMatch(temp))
                    temp = temp.Substring(0, temp.Length - 1);

                if (trailingDots)
                    temp += "...";
            }

            return temp;
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.