如何最小化String中的空间 [英] How to minimize space in String

查看:68
本文介绍了如何最小化String中的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

String prod =披萨咖啡馆,我想打印PizzaCafe。如何在Asp.net中做到这一点?我想减少披萨和咖啡馆之间的空间?

解决方案

你需要替换里面的空格。所以你用string.replace做到这一点:



  string  originalString =   Pizza Cafe; 
string minString = originalString.Replace( );

// minString现在包含PizzaCafe





要添加标题大小写,您可以这样做:



  string  originalString =   pizza pizza ; 
string minTitleString = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(originalString).Replace( ) ;

// minTitleString =PizzaCafe





很长,但如果你在顶部添加使用System.Globalization的引用,它可以缩短为:



  string  minTitleString = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(originalString).Replace(  ); 


修剪只从开始或结束中删除空格尝试使用替换建议Ron


< blockquote>你可以使用split函数在空格处分割单词,然后将每个单词的第一个字母转换为大写,然后再将它们全部合并(可能在循环中使用StringBuilder)。



代码留给读者练习。


String prod="pizza cafe",i want to print "PizzaCafe" .How to do that in Asp.net?i want to minimize space between pizza and cafe?

解决方案

You need to replace the spaces inside the string. so you do that with string.replace:

string originalString = "Pizza Cafe";
string minString = originalString.Replace(" ", "");

//minString now contains "PizzaCafe"



To add title casing, you can do this:

string originalString = "pizza cafe";
string minTitleString = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(originalString).Replace(" ", "");

//minTitleString = "PizzaCafe"



Its long, but if you add a reference "using System.Globalization" at the top, it can be shortened to:

string minTitleString = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(originalString).Replace(" ", "");


Trim only removes spaces from start or end try to use replace as suggested Ron


You can use split function to split words at space and then convert to uppercase the first letter of each word and then merge them all again (possibly using a StringBuilder in a loop).

Code left as an exercise to the reader.


这篇关于如何最小化String中的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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