快速字符串问题 [英] Quick string question

查看:64
本文介绍了快速字符串问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我觉得有一个简单的答案。如果超过9个字符,我需要在字符串中返回第一个

9个字符,否则

整个字符串。


如果希望这段代码能做到这一点:


Dim myString as string =" string"

msgbox(mystring.substring(0,9) )


但这给了我一个索引和长度必须引用

字符串中的一个位置。错误。


我明白我可以用if语句做到这一点,但我想知道是否

有一个更简单的.Net这样做的方式


有什么想法吗?


问候


Niclas

Hi,

I have a feeling there is a easy answer to this. I need to return the first
9 characters in a string if it is longer than 9 characters, otherwise the
whole string.

If was hoping this code would do this:

Dim myString as string="string"
msgbox (mystring.substring(0,9))

But this gives me a "Index and length must refer to a location within the
string." error.

I understand that i could do this with if statements, but i was wondering if
there is a easier ".Net" way to do this

Any ideas ?

Regards

Niclas

推荐答案

这样的事情怎么样?没什么.Net关于它,真的,只是一种方式

完成任务而不使用If:


函数First9Method(byval SomeString as string)as String


SomeString& ="

返回SomeString.Substring(0,9).Trim


结束功能


但是得到了什么反对If声明?

" MS Newsgroups" <无**** @ nospam.com>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP09.phx.gbl ...
How about something like this? Nothing ".Net" about it, really, just a way
of accomplishing the task without using an If:

Function First9Method(byval SomeString as string) as String

SomeString &= " "
return SomeString.Substring(0, 9).Trim

End Function

But whatcha got against If statements?
"MS Newsgroups" <no****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...


我觉得有一个简单的答案。如果长度超过9个字符,我需要在字符串中返回
前9个字符,否则
整个字符串。

如果希望这段代码可以这样做:

Dim myString as string =" string"
msgbox(mystring.substring(0,9))

但这给了我一个索引和长度必须请参阅
字符串中的位置。错误。

我明白我可以用if语句做到这一点,但如果有一个更简单的.Net,我想知道
。方法这样做

任何想法?

关于

Niclas
Hi,

I have a feeling there is a easy answer to this. I need to return the first 9 characters in a string if it is longer than 9 characters, otherwise the
whole string.

If was hoping this code would do this:

Dim myString as string="string"
msgbox (mystring.substring(0,9))

But this gives me a "Index and length must refer to a location within the
string." error.

I understand that i could do this with if statements, but i was wondering if there is a easier ".Net" way to do this

Any ideas ?

Regards

Niclas





无论如何,我建议使用IF语句,例如,

有趣。在C中做这些事情的方法,但它们往往掩盖了代码的含义。同样适用于任何语言。


例如,哪个对读者更有意义?两者都没有比另一个更快




int c;


c =(d> 0?1 :-1)





如果d> 0

{

c = 1

}

其他

{

c = -1

}


" MS Newsgroups" <无**** @ nospam.com>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP09.phx.gbl ...

I would suggest using IF statements anyway, there are, for example,
"interesting" ways of doing these things in C, but they tend to obscure the
meaning of the code. Same applies in any language really.

For example, which is more meaningful to the reader? Neither is quicker
than the other.

int c;

c = ( d > 0 ? 1 : -1 )

or

if d > 0
{
c = 1
}
else
{
c = -1
}

"MS Newsgroups" <no****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...


我觉得有一个简单的答案。如果长度超过9个字符,我需要在字符串中返回
前9个字符,否则
整个字符串。

如果希望这段代码可以这样做:

Dim myString as string =" string"
msgbox(mystring.substring(0,9))

但这给了我一个索引和长度必须请参阅
字符串中的位置。错误。

我明白我可以用if语句做到这一点,但如果有一个更简单的.Net,我想知道
。方法这样做

任何想法?

关于

Niclas
Hi,

I have a feeling there is a easy answer to this. I need to return the first 9 characters in a string if it is longer than 9 characters, otherwise the
whole string.

If was hoping this code would do this:

Dim myString as string="string"
msgbox (mystring.substring(0,9))

But this gives me a "Index and length must refer to a location within the
string." error.

I understand that i could do this with if statements, but i was wondering if there is a easier ".Net" way to do this

Any ideas ?

Regards

Niclas



事后看来,如果你的

字符串合法地以你想要保留的空格结束,我想你会被搞砸。然后你会在修剪后用空格填充

,直到长度为9.这对你和处理器更方便了吗?只使用一个If?

" e" < e@e.com>在消息新闻中写道:qJ ******************** @ speakeasy.net ...
In hindsight though, I guess you''d be screwed with that method if your
string legitimately ended in spaces you wanted to preserve. Then you''d have
to pad it with spaces after the trim, until the length is 9. Wouldn''t it be
way easier for you and the processor to just use an If?

"e" <e@e.com> wrote in message news:qJ********************@speakeasy.net...
这样的事情怎么样?没什么.Net关于它,真的,只是一个
方式来完成任务而不使用If:

函数First9Method(byval SomeString as string)as String

SomeString& =
返回SomeString.Substring(0,9).Trim

结束功能

但是什么反对If语句?

MS新闻组 <无**** @ nospam.com>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP09.phx.gbl ...
How about something like this? Nothing ".Net" about it, really, just a way of accomplishing the task without using an If:

Function First9Method(byval SomeString as string) as String

SomeString &= " "
return SomeString.Substring(0, 9).Trim

End Function

But whatcha got against If statements?
"MS Newsgroups" <no****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...


我觉得有一个简单的答案。我需要在一个字符串中返回第一个
Hi,

I have a feeling there is a easy answer to this. I need to return the first
9个字符,如果长度超过9个字符,否则
整个字符串。

如果希望这个代码会这样做:

Dim myString as string =" string"
msgbox(mystring.substring(0,9))

但是这给了我一个索引和长度必须指向
字符串中的位置。错误。

我明白我可以使用if语句执行此操作,但我
9 characters in a string if it is longer than 9 characters, otherwise the whole string.

If was hoping this code would do this:

Dim myString as string="string"
msgbox (mystring.substring(0,9))

But this gives me a "Index and length must refer to a location within the string." error.

I understand that i could do this with if statements, but i was



想知道


wondering if

有一个更简单的.Net方法这样做

任何想法?

关于

Niclas
there is a easier ".Net" way to do this

Any ideas ?

Regards

Niclas




这篇关于快速字符串问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆