How to paste formatted text in textbox, C# - c#

am trying to paste some emails into a large textbox with a semi-colon(;) at the end of each email but i don't want the semi-colon to be at the back of the last email. Please how can i go about this, all answers would be welcomed.

You can use the TrimEnd() function
string Emails = "me#email.com;metoo#email.com;";
this.textBox1.Text = Emails.TrimEnd(';');
or if it's already in your TextBox
this.textBox1.Text = this.textBox1.Text.TrimEnd(';');

Try this
string email = "metoo#email.com;abc#email.com;xyz#email.com;";
this.textBox1.text = email.Replace(";", string.Empty);
//to show emails in separate lines then use it in this way
this.textBox1.text = email.TrimEnd(';').Replace(";", Environment.NewLine);

Just remove the last char. You can do that with a substring.

I don't understand what do you want. But maybe this code can help you to remove the last semi-colon if it exists.
var str = "email#email.com;email2#email.com;email3#email.com;";
var length = str.Length - 1;
if (str.LastIndexOf(';') == length)
{
str.Remove(length);
}

Related

Inserting a certain character in between every character in a textbox

So in this example I attempted to make a program that inserts a "*" in between every character that is in a certain textbox (textBox1). Though I'm having trouble figuring it out. Yes I have looked around on stack-overflow already, nothing seems to relate/work with what I'm trying to do.
Here's the code I have currently:
for (int i = 1; i <= textBox1.Text.Length; i += 1)
{
textBox2.Text = textBox1.Text.Insert(i, "*");
i++;
}
Here is a picture of what this current code does:
pretty easy to do like...
textBox2.Text = string.Join('*', textBox1.Text.ToCharArray())
You're doing a whole lot of unnecessary things. I'm using two strings instead of text boxes as I'm on Console, but the idea is the same.
var tb1Text = "hello";
var tb2Text = string.Empty;
foreach (var ch in tb1Text)
{
tb2Text += ch + "*";
}
tb2Text = tb2Text.TrimEnd(new char[] { '*' });
We iterate through the string (because a string is essentially an array of char), and add it to the tb2Text along with a trailing *. If you don't want a trailing * at the very end, use the last line of code, otherwise get rid of it.
Result
tb1Text = hello
And
tb2Text = h*e*l*l*o

Remove specific entire word from a string

I have this text file that contains these 3 lines:
Bob
MikeBob
Mike
How could I remove 'Mike' without removing 'Mike' from 'MikeBob'?
I have tried this:
string text = File.ReadAllText("C:/data.txt");
text = text.Replace("Mike", "");
But it removed all occurrences of Mike.
What should I do?
var text = Regex.Replace(File.ReadAllText("C:/data.txt"), "\bMike\b","");
Pretty easy through regex.
// input string
String str = "Hello.???##.##$ here,#$% my%$^$%^&is***()&% this";
// similar to Matcher.replaceAll
str = Regex.Replace(str,#"[^\w\d\s]","");

How can you trimend of textbox text from another textbox text

I have two textbox's textbox1 (read/write) & textbox2 (read only), textbox1 is where you enter text at which point you press a button to pass that text into textbox2 (textbox2 continually concatenates the text from textbox1). I have a second button that I want to press to remove the text from textbox2 that is still in textbox1. Can anyone suggest how to do this?
I was thinking;
string one = textbox1.text;
string two = textbox2.text;
string newTwo = two.trimend( value of string 'one' needs to be removed );
textbox2.text = newTwo;
textbox1;
world
textbox2;
hello world
I know this sounds odd but it's a bit of a work around for an algebra calculator.
If (textbox2.Text.EndsWith(textbox1.Text))
textbox2.Text = textbox2.Text.Substring(0, textbox2.Text.Length - textbox1.Text.Length);
you can do :
var start = two.IndexOf(one);
textbox2.text = two.Substring(start);
You can also use Replace method
string newTwo = two.Replace(one,"");
As far as i undestand your question boils down to "How to implement TrimEnd?". You can use code like this:
public static class StringExtensions
{
public static string TrimEnd(this string str,
string subject,
StringComparison stringComparison)
{
var lastIndexOfSubject = str.LastIndexOf(subject, stringComparison);
return (lastIndexOfSubject == -1
|| (str.Length - lastIndexOfSubject) > subject.Length)
? str
: str.Substring(0, lastIndexOfSubject);
}
}
Then is your code-behind:
textbox2.text = textbox2.text.TrimEnd(textbox1.text, StringComparison.CurrentCulture);
#jayvee is right. Replacing with string.Empty would do.
But there's one problem with that:
When replacing "Hello" in "Hello World" the leading whitespace would remain. newTwo would be " World". You may also would want to replace multiple whitespaces via Regex as posted here and then Trim() the new string.
Also this is case sensitive.

How to get rid of unwanted spaces after using ToString() in C#?

This might be a problem with Session and not ToString(), I'm not sure.
I have two .aspx pages and I want to pass an IP address from a datatable from one page to the other. When I do this, spaces get added that I don't want. The simple version of the code is this:
first .aspx page
int num = DropDownList1.SelectedIndex;
DataView tempDV = SqlDataSource2.Select(DataSourceSelectArguments.Empty) as DataView;
Session["camera"] = tempDV.Table.Rows[num].ItemArray[2];
Response.Redirect("test.aspx");
test.aspx page
string ipCamAdd = Session["camera"].ToString();
TextBox1.Text = "http://" + ipCamAdd + "/jpg/image.jpg?resolution=320x240";
what I want to print is
http ://ipadd/jpg/image.jpg?resolution=320x240
but what prints out is
http//ipaddress /jpg/image.jpg?resolution=320x240
how can I fix this?
Also, I asked this question hoping someone could tell me why this is happening as well. Sorry for the mistake.
Try this:
string ipCamAdd = Session["camera"].Trim().ToString();
For the valid concern, Session["camera"] could be null, add function such as the following to your code
static string ToSafeString(string theVal)
{
string theAns;
theAns = (theVal==null ? "" : theVal);
return theAns;
}
Then use:
string ipCamAdd = Session["camera"].ToSafeString().Trim();
You can use string.Replace if you just want to get rid of the spaces:
TextBox1.Text = "http://" + (ipCamAdd ?? "").Replace(" ", "") + "/jpg/image.jpg?resolution=320x240";
Trim the result before setting to session.
Session["camera"] = tempDV.Table.Rows[num].ItemArray[2].Trim();
Seems In SQL your data type is char(*) if you convert the data type to varchar and re enter data, you wont get any additional spaces

Clipboard can copy a few words?

I'm weak in English so I hope you will understand this.
I learned yesterday that Clipboard is copy.
//textBox1.Text = "My name is not exciting";
Clipboard.SetText(textBox1.Text);
textBox2.Text = Clipboard.GetText();
This code copies your everything from the textbox1 and paste it in textbox2 right?
So it's possible to copy only a few words from textbox1 and paste it in textbox2?
If you don't understand, I'm want copy only a few words not all the line.
Even if this high level code still bring me :)
Clipboard.GetText(); will return the raw copied elements.
What you can do is save them to some variable:
string text = Clipboard().GetText();
Then do something with text, to get the elements you need:
textBox2.Text = text.Substring(0, 10); // An example.
The main idea to take away from this is, GetText() will give you a string. It's up to you to slice and dice that string any way you see fit and then make use of the results.
You don't need the Clipboard for this. Your user won't like that ;)
Just create a variable like this:
string box1Content = textBox1.Text;
textBox2.Text = boxContent;
You can even skip that variable.
If you really want to use the clipboard, your way is doing that.
For just getting some text out of the textbox you can either use substring or regular expressions.
http://msdn.microsoft.com/en-us/library/aka44szs.aspx
Good luck
Daniel, the substring method is a good one to use. You simply tell it where you want to take a piece of your text and it will create a new string of just that.
textBox1.Text = "MY name is not exciting";
//pretend you only want "not exciting" to show
int index = textBox1.Text.IndexOf("not");//get the index of where "not" shows up so you can cut away starting on that word.
string partofText = textBox1.Text.Substring(index);//substring uses the index (in this case, index of "not") to take a piece of the text.
Clipboard.SetText(partofText);
textBox2.Text = Clipboard.GetText();
To my mind is better idea to take selected text from text box.
I'm not sure witch kind of text box you are using but so show example on WPF you should use TextBox.SelectedText property.
I like linq. :-)
This is a example for splitting the string, enumerable it and concats in one:
textBox1.Text = "My name is not exciting";
int firstWord = 2;
int lastWord = 4;
string[] wordList = textBox1.Text.Split(new[] { ' ', '.', ',' }, StringSplitOptions.RemoveEmptyEntries);
string newText = string.Concat(wordList.Where((word, count) => count >= firstWord - 1 && count < lastWord).Select(w => w + " ")).TrimEnd();
Clipboard.SetText(newText);
textBox2.Text = Clipboard.GetText();
// Result: "name is not"
Edit: and without Clipboard you can use simply this Line
textBox2.Text = newText;

Categories