C# .Net 相当于旧的 vb left(string, length) 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/844059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.Net equivalent of the old vb left(string, length) function?
提问by Josh
As a non .net programmer I'm looking for the .net equivalent of the old vb function left(string, length)
. It was lazy in that it worked for any length string. As expected, left("foobar", 3) = "foo"
while, most helpfully, left("f", 3) = "f"
.
作为非 .net 程序员,我正在寻找与旧 vb 函数等效的 .net 函数left(string, length)
。它很懒惰,因为它适用于任何长度的字符串。正如预期的那样,left("foobar", 3) = "foo"
虽然最有帮助,left("f", 3) = "f"
.
In .net string.Substring(index, length)
throws exceptions for everything out of range. In Java I always had the Apache-Commons lang.StringUtils handy. In Google I don't get very far searching for string functions.
在 .net 中string.Substring(index, length)
,所有超出范围的东西都会抛出异常。在 Java 中,我总是使用 Apache-Commons lang.StringUtils。在 Google 中,我对字符串函数的搜索并不多。
Edit:
编辑:
@Noldorin- Wow, thank you for your vb.net extensions! My first encounter, although it took me several seconds to do the same in c#:
@Noldorin- 哇,谢谢你的 vb.net 扩展!我的第一次遇到,虽然我花了几秒钟在 c# 中做同样的事情:
public static class Utils
{
public static string Left(this string str, int length)
{
return str.Substring(0, Math.Min(length, str.Length));
}
}
Note the static class and method as well as the this
keyword. Yes, they are as simple to invoke as "foobar".Left(3)
. See also c# extensions on msdn.
注意静态类和方法以及this
关键字。是的,它们就像调用一样简单"foobar".Left(3)
。另请参阅msdn 上的 c# 扩展。
采纳答案by Noldorin
Here's an extension method that will do the job.
这是一个可以完成这项工作的扩展方法。
<System.Runtime.CompilerServices.Extension()> _
Public Function Left(ByVal str As String, ByVal length As Integer) As String
Return str.Substring(0, Math.Min(str.Length, length))
End Function
This means you can use it just like the old VB Left
function (i.e. Left("foobar", 3)
) or using the newer VB.NET syntax, i.e.
这意味着您可以像使用旧的 VBLeft
函数(即Left("foobar", 3)
)或使用较新的 VB.NET 语法一样使用它,即
Dim foo = "f".Left(3) ' foo = "f"
Dim bar = "bar123".Left(3) ' bar = "bar"
回答by RobS
You can either wrap the call to substring in a new function that tests the length of it as suggested in other answers (the right way) or use the Microsoft.VisualBasic namespace and use left directly (generally considered the wrong way!)
您可以将调用 substring 包装在一个新函数中,按照其他答案中的建议测试它的长度(正确的方式),或者使用 Microsoft.VisualBasic 命名空间并直接使用 left(通常被认为是错误的方式!)
回答by Jean-Bernard Pellerin
you could make your own
你可以自己做
private string left(string inString, int inInt)
{
if (inInt > inString.Length)
inInt = inString.Length;
return inString.Substring(0, inInt);
}
edit: mine is in C#, you will have to change it for vb
编辑:我的是 C#,你必须为 vb 改变它
回答by Oded
Another one line option would be something like the following:
另一个单行选项如下所示:
myString.Substring(0, Math.Min(length, myString.Length))
Where myString is the string you are trying to work with.
其中 myString 是您尝试使用的字符串。
回答by Garry Shutler
Add a reference to the Microsoft.VisualBasic library and you can use the Strings.Leftwhich is exactlythe same method.
添加对 Microsoft.VisualBasic 库的引用,您可以使用Strings.Left,这是完全相同的方法。
回答by CCondron
don't forget the null case
不要忘记空情况
public static string Left(this string str, int count)
{
if (string.IsNullOrEmpty(str) || count < 1)
return string.Empty;
else
return str.Substring(0,Math.Min(count, str.Length));
}
回答by Brad Mathews
Another technique is to extend the string object by adding a Left() method.
另一种技术是通过添加 Left() 方法来扩展字符串对象。
Here is the source article on this technique:
这是有关此技术的源文章:
http://msdn.microsoft.com/en-us/library/bb384936.aspx
http://msdn.microsoft.com/en-us/library/bb384936.aspx
Here is my implementation (in VB):
这是我的实现(在 VB 中):
Module StringExtensions
模块字符串扩展
<Extension()>
Public Function Left(ByVal aString As String, Length As Integer)
Return aString.Substring(0, Math.Min(aString.Length, Length))
End Function
End Module
终端模块
Then put this at the top of any file in which you want to use the extension:
然后将其放在要使用扩展名的任何文件的顶部:
Imports MyProjectName.StringExtensions
Use it like this:
像这样使用它:
MyVar = MyString.Left(30)
回答by danfolkes
I like doing something like this:
我喜欢做这样的事情:
string s = "hello how are you";
s = s.PadRight(30).Substring(0,30).Trim(); //"hello how are you"
s = s.PadRight(3).Substring(0,3).Trim(); //"hel"
Though, if you want trailing or beginning spaces then you are out of luck.
但是,如果您想要尾随或开头的空格,那么您就不走运了。
I really like the use of Math.Min, it seems to be a better solution.
我真的很喜欢 Math.Min 的使用,它似乎是一个更好的解决方案。
回答by Jeff Crawford
using System;
public static class DataTypeExtensions
{
#region Methods
public static string Left(this string str, int length)
{
str = (str ?? string.Empty);
return str.Substring(0, Math.Min(length, str.Length));
}
public static string Right(this string str, int length)
{
str = (str ?? string.Empty);
return (str.Length >= length)
? str.Substring(str.Length - length, length)
: str;
}
#endregion
}
Shouldn't error, returns nulls as empty string, returns trimmed or base values. Use it like "testx".Left(4) or str.Right(12);
不应该出错,返回空字符串作为空字符串,返回修剪或基值。像 "testx".Left(4) 或 str.Right(12) 一样使用它;
回答by deadManN
Just In Very Special Case:
只是在非常特殊的情况下:
If you are doing this left so then you will check the data with some partial string, for example:
if(Strings.Left(str, 1)=="*") ...;
如果您这样做,那么您将使用一些部分字符串检查数据,例如:
if(Strings.Left(str, 1)=="*") ...;
Then you can also use C# instance methods such as StartsWith
and EndsWith
to perform these tasks.
if(str.StartsWith("*"))...;
然后,您还可以使用 C# 实例方法(例如StartsWith
和 )EndsWith
来执行这些任务。
if(str.StartsWith("*"))...;