vb.net 中字符串中字符的左边

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1461025/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:26:56  来源:igfitidea点击:

Left of a character in a string in vb.net

vb.net

提问by acadia

say if I have a string 010451-09F2

说如果我有一个字符串010451-09F2

How to I get left of - from the above string in vb.net

如何从 vb.net 中的上述字符串离开 -

I want 010451

我要010451

The left function doesn't allow me to specify seperator character.

left 函数不允许我指定分隔符。

Thanks

谢谢

回答by Reed Copsey

Given:

鉴于:

Dim strOrig = "010451-09F2"

You can do any of the following:

您可以执行以下任一操作:

Dim leftString = strOrig.Substring(0, strOrig.IndexOf("-"))

Or:

或者:

Dim leftString = strOrig.Split("-"c)(0) ' Take the first index in the array

Or:

或者:

Dim leftString = Left(strOrig, InStr(strOrig, "-"))
' Could also be: Mid(strOrig, 0, InStr(strOrig, "-"))

回答by Meta-Knight

Dim str As String = "010451-09F2"
Dim leftPart As String = str.Split("-")(0)

Split gives you the left and right parts in a string array. Accessing the first element (index 0) gives you the left part.

拆分为您提供字符串数组中的左右部分。访问第一个元素(索引 0)为您提供左侧部分。

回答by JL.

Sorry not sure on the vb syntax, but the c# is

抱歉不确定 vb 语法,但 c# 是

 string mystring ="010451-09F2";
 string whatIwant = mystring.Split('-')[0];

回答by shahkalpesh

dim s as String = "010451-09F2"
Console.WriteLine(s.Substring(0, s.IndexOf("-")))
Console.WriteLine(s.Split("-")(0))

回答by Sarkazein

Get the location of the dash first (or do it inline), and use that value for the left. This is old school VBA, but it'll be something like this:

首先获取破折号的位置(或内联),并将该值用于左侧。这是老式的 VBA,但它会是这样的:

Left(YourStringWithTheDash, InStr(YourStringWithTheDash)-1)

Left(YourStringWithTheDash, InStr(YourStringWithTheDash)-1)

回答by Nirlep

Use something like this:

使用这样的东西:

Mid("010451-09F2",1,InStr("-"))

Mid("010451-09F2",1,InStr("-"))

回答by AMissico

    Dim sValue As String = "010451-09F2"
    Debug.WriteLine(sValue.Substring(0, sValue.IndexOf("-"c)))

回答by Nathan Woodburn

This helped

这有助于

Dim str1 as string = [email protected]
Dim str As String

str = Strings.Left(str1, str1.LastIndexOf("@"))