复制粘贴时如何修剪空格? [英] How to trim spaces when copy paste?

查看:122
本文介绍了复制粘贴时如何修剪空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多行文本框现在我从excel单元格复制值并粘贴到多行文本框。内容也被粘贴,但也粘贴空格。所以内容转到upside.so文本框视图没有内容不可用在文本框中。



双击单元格并复制内容并正确粘贴它。但是整个单元格被复制了未修剪的空格(这是出于用户友好的目的) )。



我尝试过:



我用过textbox1 .text.trim()方法,但这不起作用。

I have multiline textbox now i copy the value from excel cell and paste to multi line textbox.Content is pasted but also paste the empty spaces also.so the content go to upside.so textbox is view like no contents are not available in textbox.

Double click the cell and copy the content and paste it paste correctly.but the whole cell is copied the spaces not trimmed(this is for user friendly purpose).

What I have tried:

I have used textbox1.text.trim() method but this is not working.

推荐答案

当用户复制和粘贴时,你不能修剪sapces,什么相反,当你提交表单并且正在阅读数据时,你会修剪空格。



You don't\can't trim the sapces when the user copy and pastes, what you do instead is trim the spaces when the form is submitted and you are reading the data.

string data = TextboxMyData.Text.Trim();


(不是解决方案,而是注释 - 评论似乎不工作atm )



修剪()方法只修剪常规空间;如果在粘贴的字符串的开头有任何其他内容,则不会对其进行修剪。因此,您必须以调试模式启动程序,并找出确切的组成粘贴字符串。然后相应地对此字符串进行操作,或者让我们知道您是否仍然卡住了。请:)
(not a solution, but a comment - comments do not seem to work atm)

Trim() method will only trim regular spaces; if there is anything else at the beginning of the pasted string, it will not be trimmed. So, you will have to launch your program in debug mode, and find out of what exactly is composed pasted string. Then act on this string accordingly, or let us know if you still are stuck. Kindly :)


如果你的意思是你想要修剪去除空白行,那么不,Trim不会这样做 - 它只删除字符串末尾的空格,而不是中间的空格,多行TextBox的Text属性返回一个字符串,每行由\\\\ n分隔 - 所以Trim看不到空行。

相反,试试这个:

If you mean you want trim to remove blank lines, then no, Trim will not do that - it only removes whitespace at the ends of strings, not from the middle, and the Text property of a multiline TextBox returns a single string, which each line delimited by "\r\n" - so blank lines aren't seen by Trim.
Instead, try this:
string text = Clipboard.GetText();
string[] parts = text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
tbOutData.Text = string.Join("\r\n", parts.Select(p => p.Trim()).Where(w => !string.IsNullOrWhiteSpace(w)));


这篇关于复制粘贴时如何修剪空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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