C#Trim()与replace() [英] C# Trim() vs replace()

查看:123
本文介绍了C#Trim()与replace()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C# string 中,如果我们想将字符串中的" 替换为 string.empty ,可以使用 stringValue.Trim() stringValue.replace(",string.empty).两者具有相同的目的.但是哪个更好?

In a C# string if we want to replace " " in a string to string.empty, is it fine to use stringValue.Trim() or stringValue.replace(" ", string.empty). Both serve the same purpose. But which one is better?

推荐答案

Trim() Replace()的作用不同.

Trim()删除字符串开头和结尾的所有空格字符.这意味着空格标签换行返回和其他各种空白字符.

Trim() removes all whitespace characters from the beginning and end of the string. That means spaces, tabs, new lines, returns, and other assorted whitespace characters.

Replace()仅将指定的字符替换为给定的替换.因此, Replace(",string.empty)仅将空格替换为空字符串.Replace()还会用给定的替换项替换指定字符串的所有实例,而不仅仅是在字符串开头和结尾处的那些实例.

Replace() only replaces the designated characters with the given replacement. So Replace(" ", string.empty) will only replace spaces with empty strings. Replace() also replaces all instances of the designated string with the given replacement, not just those at the beginning and end of the string.

这篇关于C#Trim()与replace()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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