|
My tantrum is directeed to the pile of sunshine that is the .NET framework, in fact I usually make my own libraries anyway. Still for a "framework" so bloated not having a simple and fundamental function is basically an indication of utter failure.
GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X
If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver
When I was six, there were no ones and zeroes - only zeroes. And not all of them worked. -- Ravi Bhavnani
|
|
|
|
|
Yes, just like you, I wouldn't use the .NET Framework for anything, obviously!
It doesn't even have a .Left() method on string, for god's sake man! Who make that sh*t?!
|
|
|
|
|
Super Lloyd wrote: you are the quick to hate type, are you not? No.
Super Lloyd wrote: One tiny method missing and boom I hate you MS! I'm trying to fit in with the Lounge crowd.
Super Lloyd wrote: public static class StringExtensions {
public static string Left(this string s, int n) {
if (n >= s.Length)
return "";
return s.Substring(s.Length - n);
}
} You tried to sneak that one there. return "". How dare you.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
While I cherish the myriad weirdnesses I am aware of in my long-term relationship with C#, the absence of 'Left would not qualify. Welcome to Linq: [^].
Unless there's something you just can't stand about the word: "Take" ?
cheers, Bill
«There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008
|
|
|
|
|
These are the string extension methods I wrote ages ago (well, since extension methods have been around):
(Note that some of this isn't very elegant, could be LINQ'ified, could be optimized, etc.)
public static string SingleQuote(this String src)
{
return "'" + src + "'";
}
public static string Quote(this String src)
{
return "\"" + src + "\"";
}
public static string ExchangeQuoteSingleQuote(this String src)
{
string ret = src.Replace("'", "\0xFF");
ret = ret.Replace("\"", "'");
ret = ret.Replace("\0xFF", "\"");
return ret;
}
public static string Spaced(this String src)
{
return " " + src + " ";
}
public static string Parens(this String src)
{
return "(" + src + ")";
}
public static string Brackets(this String src)
{
return "[" + src + "]";
}
public static string CurlyBraces(this String src)
{
return "{" + src + "}";
}
public static string Between(this string src, char start, char end)
{
string ret = String.Empty;
int idxStart = src.IndexOf(start);
if (idxStart != -1)
{
++idxStart;
int idxEnd = src.IndexOf(end, idxStart);
if (idxEnd != -1)
{
ret = src.Substring(idxStart, idxEnd - idxStart);
}
}
return ret;
}
public static string Between(this string src, string start, string end)
{
string ret = String.Empty;
int idxStart = src.IndexOf(start);
if (idxStart != -1)
{
idxStart += start.Length;
int idxEnd = src.IndexOf(end, idxStart);
if (idxEnd != -1)
{
ret = src.Substring(idxStart, idxEnd - idxStart);
}
}
return ret;
}
public static string BetweenEnds(this string src, char start, char end)
{
string ret = String.Empty;
int idxStart = src.IndexOf(start);
if (idxStart != -1)
{
++idxStart;
int idxEnd = src.LastIndexOf(end);
if (idxEnd != -1)
{
ret = src.Substring(idxStart, idxEnd - idxStart);
}
}
return ret;
}
public static int Count(this string src, char find)
{
int ret = 0;
foreach (char s in src)
{
if (s == find)
{
++ret;
}
}
return ret;
}
public static string Surrounding(this String src, string s)
{
return src.LeftOf(s) + src.RightOf(s);
}
public static string RightOf(this String src, char c)
{
string ret = String.Empty;
int idx = src.IndexOf(c);
if (idx != -1)
{
ret = src.Substring(idx + 1);
}
return ret;
}
public static bool BeginsWith(this String src, string s)
{
return src.StartsWith(s);
}
public static string RightOf(this String src, string s)
{
string ret = String.Empty;
int idx = src.IndexOf(s);
if (idx != -1)
{
ret = src.Substring(idx + s.Length);
}
return ret;
}
public static string RightOfRightmostOf(this string src, char c)
{
string ret = String.Empty;
int idx = src.LastIndexOf(c);
if (idx != -1)
{
ret = src.Substring(idx + 1);
}
return ret;
}
public static string LeftOf(this string src, char c)
{
string ret = src;
int idx = src.IndexOf(c);
if (idx != -1)
{
ret = src.Substring(0, idx);
}
return ret;
}
public static string LeftOf(this String src, string s)
{
string ret = src;
int idx = src.IndexOf(s);
if (idx != -1)
{
ret = src.Substring(0, idx);
}
return ret;
}
public static string NullIfEmpty(this String src)
{
return String.IsNullOrEmpty(src) ? null : src;
}
public static string LeftOf(this String src, int n)
{
return src.Length < n ? src : src.Substring(n);
}
public static string LeftOfRightmostOf(this String src, char c)
{
string ret = src;
int idx = src.LastIndexOf(c);
if (idx != -1)
{
ret = src.Substring(0, idx);
}
return ret;
}
public static string LeftOfRightmostOf(this String src, string s)
{
string ret = src;
int idx = src.IndexOf(s);
int idx2 = idx;
while (idx2 != -1)
{
idx2 = src.IndexOf(s, idx + s.Length);
if (idx2 != -1)
{
idx = idx2;
}
}
if (idx != -1)
{
ret = src.Substring(0, idx);
}
return ret;
}
public static string RightOfRightmostOf(this String src, string s)
{
string ret = src;
int idx = src.IndexOf(s);
int idx2 = idx;
while (idx2 != -1)
{
idx2 = src.IndexOf(s, idx + s.Length);
if (idx2 != -1)
{
idx = idx2;
}
}
if (idx != -1)
{
ret = src.Substring(idx + s.Length, src.Length - (idx + s.Length));
}
return ret;
}
public static char Rightmost(this String src)
{
char c = '\0';
if (src.Length > 0)
{
c = src[src.Length - 1];
}
return c;
}
public static string TrimLastChar(this String src)
{
string ret = String.Empty;
int len = src.Length;
if (len > 0)
{
ret = src.Substring(0, len - 1);
}
return ret;
}
public static bool IsBlank(this string src)
{
return String.IsNullOrEmpty(src) || (src.Trim() == String.Empty);
}
public static string Contains(this String src, string[] tokens)
{
string ret = String.Empty;
int firstIndex = 9999;
foreach (string token in tokens)
{
int idx = src.IndexOf(token);
if ((idx != -1) && (idx < firstIndex))
{
ret = token;
firstIndex = idx;
}
}
return ret;
}
public static string SplitCamelCase(this string input)
{
return Regex.Replace(
Regex.Replace(input, @"(\P{Ll})(\P{Ll}\p{Ll})", "$1 $2"),
@"(\p{Ll})(\P{Ll})", "$1 $2");
}
public static string CamelCase(this string src)
{
return src[0].ToString().ToLower() + src.Substring(1).ToLower();
}
public static string PascalCase(this string src)
{
string ret = String.Empty;
if (!String.IsNullOrEmpty(src))
{
ret = src[0].ToString().ToUpper() + src.Substring(1).ToLower();
}
return ret;
}
public static string PascalCaseWords(this string src)
{
StringBuilder sb = new StringBuilder();
string[] s = src.Split(' ');
string more = String.Empty;
foreach (string s1 in s)
{
sb.Append(more);
sb.Append(PascalCase(s1));
more = " ";
}
return sb.ToString();
}
|
|
|
|
|
Yesterday I noticed one of our projects has a reference to Microsoft.VisualBasic. What the...?
Upon closer inspection I found it was needed for new My.Computer.Devices.ComputerInfo().TotalPhysicalMemory() .
Looked around on the web, and there really isn't an easy C# variant
|
|
|
|
|
While MS gave you the power of Linq in C#....this gives you an infinite amount of possibilities.
That combined with extensions, and you are so much better off than with VB! It's true, you'll have to spend 2 minutes of your personal precious time to write this code once, and to remember to add this extension 'lib' to all your projects where you might need them....boohoo.
Sth like this... one line of code...
public static string Left(this string inputString, int length) { return new string((from ch in inputString select ch).Take(length).ToArray()); }
Now you can just Left a string by doing string.Left(5)....so easy but 1000 times more powerful (and prob as many times more performant) than VB....
|
|
|
|
|
Member 10952144 wrote: you'll have to spend 2 minutes of your personal precious time to write this code Exactly!
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
Well it'll be less now if you just copy/paste the code....I think 30 secs will do now. Is prob just about as much as VB would need to show you its 'intellisense' info on the Left function
|
|
|
|
|
My point is, why after all these years have the C# builders not added it in. It would only take a few minutes of their time.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
I understand your frustrations, but to be honest, I didn't ever miss that function. I did VB6 for years, I did Java for years, even did some Delphi, but now C# for quite some years, and every language has its benefits and its downsides, but C# really is the nicest language I've used, Delphi comes in second
The reasoning behind not providing a Left function I think is since it's not a core function, but more of a "convenience function", one that would give discussions among developers, since they would make their own 'more performing' version of that function.
VB6 provided more convenience functions out of the box, since it was mainly meant to make software development 'as easy as possible'.
And to be honest, I like the fact that C# provided us with Extensions, Linq, Entity Framework,... more than the couple of arguable functions they might have 'forgotten'.
Here they were really one of the first to incorporate these new ideas, where Java now tries to catch up...
|
|
|
|
|
Member 10952144 wrote: I understand your frustrations, Actually, I don't think you do, but that is my fault. I think a lot of other people thought I was seriously "hating" on Microsoft. My original post was done tongue-in-cheek. I do find it weird that they don't have the LEFT method on a string but it doesn't actually bother me at all. I just wanted to get in on all the hating going on with something silly.
But, you do make good points.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
Left isn't a method of the String class in VB.NET either. It's hidden in the Microsoft.VisualBasic.Strings class. I work in VB.NET all the time and just created my own Left extension method, just like you would in C#.
|
|
|
|
|
C# doesn't have ANY methods on string. The methods on string are part of the .NET framework, and are the same for any .NET language - C#, VB, F#, you name it.
|
|
|
|
|
Wow, go do some string manipulations in ANSI C, that'll give you something to complain about
As a general rule, the more powerful the language the more responsibility the programmer has for implementing algorithms correctly. Checking bounds before taking a bite out of an array is basic stuff, why should the language hold your hand for that?
This kind of thing annoys me about VB, getting back a string that is shorter than the length you specified is like just having the language ignore an exception and give an arbitrary result. It's not what you asked for, and you shouldn't get it. What if code down the line is expecting the string to be that exact length?
|
|
|
|
|
StatementTerminator wrote: This kind of thing annoys me about VB, getting back a string that is shorter than the length you specified is like just having the language ignore an exception and give an arbitrary result. Not even close. It's like being a human.
If you asked someone, "hey, give me the first 100 characters of the phrase 'i love VB'", would you expect them to yell at you with an exception or to just return your string? Maybe you think too much like a computer instead of a human.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
RyanDev wrote: If you asked someone, "hey, give me the first 100 characters of the phrase 'i love VB'", would you expect them to yell at you with an exception or to just return your string?
I would expect different answers from different people, which is exactly the point. And yes, I would be the elephanting Vulcan who says that the question is unanswerable, because what is being asked for can't be given.
|
|
|
|
|
Give me the first 100 digits of 10.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
10.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
|
|
|
|
Exactly.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
Add a reference to Visual Basic and just use the left function from VB
string test = "Hello World";
string shortTest = Microsoft.VisualBasic.Strings.Left(test, 5);
Console.WriteLine(shortTest);
|
|
|
|
|
I know.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
Func<string, int, string> Left = ( s, n ) => s.PadRight( n + 1 ).Remove( n );
Console.WriteLine( Left( "FooBar", 3 ) );
Console.WriteLine( Left( "", 6 ) + "*" );
Console.ReadKey();
|
|
|
|
|
I miss VB too. Somehow it's very good at dodging bullets.
We're philosophical about power outages here. A.C. come, A.C. go.
|
|
|
|
|
Once a year the management company for the buildings on the campus where I am hold a client appreciation day and gives everyone free burgers and ice cream. You get a ticket for each. I just got given a second set of tickets.
Oh, it's in-n-out so
Gotta love a free lunch!
|
|
|
|
|