在一个字符串上使用字符串函数和更改的最佳方法是什么?

时间:2022-09-16 14:14:03

What should be the best way to write code:

什么是编写代码的最佳方式:

1)

Dim current = Request.Path
current = current.Remove(0, 1)
current = current.Replace(".aspx", "")

2)

Dim current = Request.Path.Remove(0, 1).Replace(".aspx", "")

3)

Dim current = Request.Path
Dim current2 = current.Remove(0, 1)
Dim current3 = current.Replace(".aspx", "")

Or 1-2 make no difference?

或者1-2没有区别?

4 个解决方案

#1


Those are all the same. Try

这些都是一样的。尝试

Path.GetFileNameWithoutExtension(Request.Path)

#2


All three are basically the same.

这三者基本相同。

Remember strings are immutable. Every time you call a method on a string, it allocates a new string object, so calling Remove() creates a new object, which is then used to call Replace().

记住字符串是不可变的。每次在字符串上调用方法时,它都会分配一个新的字符串对象,因此调用Remove()会创建一个新对象,然后用它来调用Replace()。

1 & 2 are basically identical. #3 is a little different because you use 3 separate variables, so you're holding on to the references to those strings so they can be used later. The garbage collector should know what to do with all 3 examples and handle them roughly the same however, whether you store the intermediate strings as variables or not.

1和2基本相同。 #3有点不同,因为你使用3个独立的变量,所以你要坚持对这些字符串的引用,以便以后可以使用它们。无论是否将中间字符串存储为变量,垃圾收集器都应该知道如何处理所有3个示例并处理它们大致相同。

I would use #2, simply because it's fewer lines of code and doesn't sacrafice readability but it's still a fairly short bit of code.

我会使用#2,因为它的代码行数较少,并且没有牺牲可读性,但它仍然是一个相当短的代码。

#3


Those calls can throw exceptions. For toy code it's okay not to check but, after you get the path, you should check against String.NullOrEmpty and the length before you call Remove. So, that's why I would avoid #2. Other than that, #1 seems cleaner unless you need to use the intermediate values (current as the path and current2) somewhere else in the method.

那些调用可以抛出异常。对于玩具代码,可以不检查,但是在获得路径之后,应该在调用Remove之前检查String.NullOrEmpty和长度。所以,这就是为什么我会避免#2。除此之外,#1看起来更干净,除非你需要在方法的其他地方使用中间值(current作为路径和current2)。

#4


Strings are immutable in .Net so you will get the same performance out of all of those methods.

字符串在.Net中是不可变的,因此您将从所有这些方法中获得相同的性能。

#1


Those are all the same. Try

这些都是一样的。尝试

Path.GetFileNameWithoutExtension(Request.Path)

#2


All three are basically the same.

这三者基本相同。

Remember strings are immutable. Every time you call a method on a string, it allocates a new string object, so calling Remove() creates a new object, which is then used to call Replace().

记住字符串是不可变的。每次在字符串上调用方法时,它都会分配一个新的字符串对象,因此调用Remove()会创建一个新对象,然后用它来调用Replace()。

1 & 2 are basically identical. #3 is a little different because you use 3 separate variables, so you're holding on to the references to those strings so they can be used later. The garbage collector should know what to do with all 3 examples and handle them roughly the same however, whether you store the intermediate strings as variables or not.

1和2基本相同。 #3有点不同,因为你使用3个独立的变量,所以你要坚持对这些字符串的引用,以便以后可以使用它们。无论是否将中间字符串存储为变量,垃圾收集器都应该知道如何处理所有3个示例并处理它们大致相同。

I would use #2, simply because it's fewer lines of code and doesn't sacrafice readability but it's still a fairly short bit of code.

我会使用#2,因为它的代码行数较少,并且没有牺牲可读性,但它仍然是一个相当短的代码。

#3


Those calls can throw exceptions. For toy code it's okay not to check but, after you get the path, you should check against String.NullOrEmpty and the length before you call Remove. So, that's why I would avoid #2. Other than that, #1 seems cleaner unless you need to use the intermediate values (current as the path and current2) somewhere else in the method.

那些调用可以抛出异常。对于玩具代码,可以不检查,但是在获得路径之后,应该在调用Remove之前检查String.NullOrEmpty和长度。所以,这就是为什么我会避免#2。除此之外,#1看起来更干净,除非你需要在方法的其他地方使用中间值(current作为路径和current2)。

#4


Strings are immutable in .Net so you will get the same performance out of all of those methods.

字符串在.Net中是不可变的,因此您将从所有这些方法中获得相同的性能。