简体   繁体   English

在Visual Basic 2010中使用字符串

[英]Working with strings in visual basic 2010

I have to create a class in Visual Basic called StringWork. 我必须在Visual Basic中创建一个名为StringWork的类。

Public Class StringWork

Now i wrote a shared function in the class called Working that can take a string or a string and Boolean. 现在,我在名为Working的类中编写了一个共享函数,该函数可以接受字符串或字符串和布尔值。

Public Shared Function Working(ByVal SingleString as string, optional BValu  as Boolean = true)as string
    if(working(SingleString))then
    'The handling of the string
    else if (working(SingleValue, BValue) then
    'do something else with string 
    end if
end function

The function I wrote is returning a string. 我写的函数返回一个字符串。 Can I access the string passed and edit characters in the string or change the position of characters? 是否可以访问传递的字符串并编辑字符串中的字符或更改字符的位置?

You use that optional parameter to determine how you handle that string: 您可以使用该可选参数来确定如何处理该字符串:

Public Shared Function Working(ByVal singleString as string, _
                               Optional bValue as Boolean = True) As String
  If bValue Then
    'Handle the true part manipulating the result string
  Else
    'Handle the false part manipulating the result string
  End If

End Function

If you call this function like this: 如果您像这样调用此函数:

Dim test As String = StringWork.Working("I am Spartacus")

it will call that Working function with bValue = true. 它将使用bValue = true调用该工作函数。

What bValue is suppose to represent isn't very clear from the code nor the post. 从代码或帖子中都不清楚bValue表示什么。

In VB.NET, and other .NET languages, strings are immutable. 在VB.NET和其他.NET语言中,字符串是不可变的。 Typically, if you need to modify a string that is passed to your method, you would return the modified string. 通常,如果需要修改传递给方法的字符串,则将返回修改后的字符串。 If however, you need it to modify the parameter, you can specify that it is a "ByRef" argument, in which case you will be able to set it to point to a new string object which will affect the variable that was passed into the method as a parameter. 但是,如果您需要它来修改参数,则可以将其指定为“ ByRef”参数,在这种情况下,您可以将其设置为指向新的字符串对象,这将影响传递给方法作为参数。 If you need a truly mutable string, you will need a character array or a StringBuilder object. 如果需要真正可变的字符串,则需要一个字符数组或一个StringBuilder对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM