Advertisement
Advertisement


Left function in c#


Question

what is the alternative for Left function in c# i have this in

Left(fac.GetCachedValue("Auto Print Clinical Warnings").ToLower + " ", 1) == "y");
2020/02/02
1
43
2/2/2020 1:35:24 PM

Accepted Answer

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);
2015/05/07
125
5/7/2015 2:37:13 AM


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.

2016/08/09

use substring function:

yourString.Substring(0, length);
2011/09/27

var value = fac.GetCachedValue("Auto Print Clinical Warnings")
// 0 = Start at the first character
// 1 = The length of the string to grab
if (value.ToLower().SubString(0, 1) == "y")
{
    // Do your stuff.
}
2011/09/27

Source: https://stackoverflow.com/questions/7574606
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]