Tag: string

不安全的代码来改变String对象的长度(通过变异!)?

由于.NET不使用C样式的null来结束字符串,我如何保留分配的字符串,但是使用不安全的代码来改变它的长度? 据我所知,.NET为每个字符串使用一个20字节的标头,大概这是存储字符串长度的地方,无论如何直接修改这个长度? 因此,.NET会将字符串保留在内存中,但是当我调用.Length它将返回我想要的.Length 。 如果这是可能的话,那么听到这种疯狂可能的副作用也会很有趣 UPDATE 我正在努力完成这个而不使用reflection。

C#IEnumerable to string

出于日志记录的目的,我想调用object []数组上每个对象的.ToString()方法。 我怎样才能以最简单的方式做到这一点? 说我有: myArray = new Object[]{“astring”,1, Customer} Log(????); 我怎样才能传递一个字符串,例如它的值等于: “astring”.ToString()+1.ToString()+Customer.ToString() 或者更好,每个值之间使用逗号。

如何实现字符串转换的时间跨度?

我试着在这里搜索,但它对我帮助不大.. 我想将 time_span 转换为字符串,我不想在几天内返回时间跨度..但只有HH:mm:ss。 怎么实现呢? 我的示例代码在这里: String time_span_par = “06:12:40”; String time_str = “18:13:59”; TimeSpan time_span_var = TimeSpan.Parse(time_span_par); TimeSpan time_span = TimeSpan.Parse(time_str); time_span = time_span.Add(time_span_var); string temp = time_span.ToString(“HH:mm:ss”);

将字符串转换为十进制神秘

我正面临着使用C#.NET从字符串转换为十进制的问题。 问题是我编写了以下代码,并在我的开发Windows 7 .NET Framework 3.5系统上正常工作。 但是,如果我在Windows 2003 Server .NET 3.5 Framework上移动应用程序,结果会有所不同。 谁能理解那里发生了什么? 码: dec = Convert.ToDecimal(strDec); Windows 7结果:85.00 Windows 2003结果:8500

使用LINQ解析字符串中的数字

是否有可能编写一个查询,我们从任何给定的字符串中获取所有可以解析为int的字符? 例如,我们有一个字符串,如: “$%^DDFG 6 7 23 1″ 结果必须是”67231” 甚至更轻微一点:我们只能获得前三个数字吗?

HTMLagilitypack没有删除所有的html标签如何有效地解决这个问题?

我使用以下方法从字符串中删除所有html: public static string StripHtmlTags(string html) { if (String.IsNullOrEmpty(html)) return “”; HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(html); return doc.DocumentNode.InnerText; } 但它似乎忽略了以下标签: […] 所以字符串基本返回: > A hungry thief who stole a rack of pork ribs from a grocery store has > been sentenced to spend 50 years in prison. Willie Smith Ward felt the > full […]

从字符串末尾删除多个char类型

我有一个构建地址字段的循环,其中一些字段在字符串的末尾可能是空的 List list = new List(); //list can contain any number of values, some of which might be “” (empty string) string returnValue = “”; for (int iRow = 1; iRow <= list.Count; iRow++) returnValue += String.Format("{0}, ", list[iRow]); returnValue = returnValue.Trim(); 我的输出是 asd, aaa, qwe, 123123, , , , , 如何从字符串中删除尾随“,”?

在C#中,检查stringbuilder是否包含子字符串的最佳方法

我有一个现有的StringBuilder对象,代码附加一些值和分隔符。 现在我想修改代码以添加逻辑,在附加我要检查的文本之前是否确实存在于字符串生成器变量中? 如果没有,那么只有附加否则忽略。 这样做的最佳方法是什么? 我是否需要将对象更改为字符串类型? 需要一种不妨碍性能的最佳方法。 public static string BuildUniqueIDList(context RequestContext) { string rtnvalue = string.Empty; try { StringBuilder strUIDList = new StringBuilder(100); for (int iCntr = 0; iCntr 0) { strUIDList.Append(“,”); } //need to do somthing like strUIDList.Contains(RequestContext.accounts[iCntr].uniqueid) then continue other wise append strUIDList.Append(RequestContext.accounts[iCntr].uniqueid); } rtnvalue = strUIDList.ToString(); } catch (Exception e) { throw; […]

想要从字符串中删除双引号

我在这里看到一些与删除双引号有关的问题。 但它没有解决我的问题。 他们告诉使用Replace或Trimfunction。 我用过,但问题仍然存在。 我的代码: DataTable dt = TypeConveriontHelper.ConvretExcelToDataTable(SourceAppFilePath); string targetPath = BatchFolderPath + @”\” + “TRANSACT.INX”; StreamWriter wrtr = null; wrtr = new StreamWriter(targetPath); for (int x = 0; x < dt.Rows.Count; x++) { string rowString = ""; for (int y = 0; y < dt.Columns.Count; y++) { if (y == dt.Columns.Count – 1) […]

在c#中获取字符串的特定部分

我有一个字符串 string a = “abc,xyz,wer”; 现在,我需要这个字符串的一部分 string b = “abc”; 在第一个逗号之前我需要一切。我怎么能得到它?