Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Left function in c# [duplicate]

Tags:

string

c#

People also ask

What is string function C?

Strings in C language are an array of characters ended with null characters ('\0'). The null character at the end of a string indicates its end and the strings are always enclosed by double quotes. In C language characters are enclosed by single quotes.

What is string in C with example?

In C programming, a string is a sequence of characters terminated with a null character \0 . For example: char c[] = "c string"; When the compiler encounters a sequence of characters enclosed in the double quotation marks, it appends a null character \0 at the end by default.

Can we use function on left side of an expression in C and C ++?

Can we use function on left side of an expression in C and C++? In C we cannot use function name at the left hand side of an expression.

What does Strcat do in C?

The strcat() function concatenates the destination string and the source string, and the result is stored in the destination string.


It sounds like you're asking about a function

string Left(string s, int left)

that will return the leftmost left characters of the string s. In that case you can just use String.Substring. You can write this as an extension method:

public static class StringExtensions
{
    public static string Left(this string value, int maxLength)
    {
        if (string.IsNullOrEmpty(value)) return value;
        maxLength = Math.Abs(maxLength);

        return ( value.Length <= maxLength 
               ? value 
               : value.Substring(0, maxLength)
               );
    }
}

and use it like so:

string left = s.Left(number);

For your specific example:

string s = fac.GetCachedValue("Auto Print Clinical Warnings").ToLower() + " ";
string left = s.Substring(0, 1);

It's the Substring method of String, with the first argument set to 0.

 myString.Substring(0,1);

[The following was added by Almo; see Justin J Stark's comment. —Peter O.]

Warning: If the string's length is less than the number of characters you're taking, you'll get an ArgumentOutOfRangeException.


Just write what you really wanted to know:

fac.GetCachedValue("Auto Print Clinical Warnings").ToLower().StartsWith("y")

It's much simpler than anything with substring.


use substring function:

yourString.Substring(0, length);