how do I replace part of text in textbox in c# - c#

I have a problem about using c# in word automation.
My problem is I want to replace part of text in a textbox,
ex: "ABC 12345" and replace 12345 by "123", as a result, "ABC 123"
But I don't know how to get part of text in textbox, I use
object firstshape = 1;
string text = w_Doc.shapes.get_Item(ref firstshape).TextFrame.TextRange.Text;
to get the original text in textbox,but i don't know how to get the range of part text.
Is there any solution to get any range of text in textbox? thanks a lot in advance!!!

You can use replace like this
string Replace = "12345";
string ReplaceWith = "123"
text = text.Replace("12345","123")

To get last 5 characters use this:
string text = w_Doc.shapes.get_Item(ref firstshape).TextFrame.TextRange.Text;
text = text.Substring(text.Length - 5, 5);
text = text.Replace(text, "123"); //to replace

Use Linq
string text = "ABC 12345";
string toReplace = text.Split().SkipWhile(x => x == "ABC").First();

Related

C# Finding numerical value from specific string

I have a string of varying length that I am trying to retrieve a number from. The format of the string is always:
"some text lines
FC = 1234
more text here
and so on"
So I know the string of numbers comes after "FC = ", and I know it finishes at the next \n. How can I return this number (which will vary in size) into a new string?
Try the following code snippet:
var str = "some text lines \nFC = 1234\n more text here and so on";
Console.WriteLine(Regex.Match(str, #"\d+\.*\d*").Value);
Thanks to all. Think I managed to find a way with Regex, based on ScareCrow's suggestion:
string rgSearch = searchString + #"\d+\.*\d*";
FC = Regex.Match(diagnostics, rgSearch).Value;
FC = FC.Replace(searchString, ""); //Leaves the number only

set brackets before and after on every hindi word using c#

I'm trying to set brackets { before and after on every words. My code is:
string a="अजंडा रस्ता भगा रक्षा hello";
a = Regex.Replace(a, #"\b[^ . ]+\b", #"{$0}");
This code is working fine in english words, but if i have hindi words in my textbox this return result like this:
{अजंड}ा {रस्त}ा {भग}ा {रक्ष}ा {hello}
Why this code split last character from word, and how to solve this problem.
Maybe you can try to use LINQ.
string a="अजंडा रस्ता भगा रक्षा hello";
a = string.Join("",from g in a.Split(' ').ToArray() select "{"+g+"}");

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]","");

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;

Populate the content in between two words in Richtextbox c#

I am very new to VS. And c#. Here I have Richtextbox. I want to filter it content which in between two words.
This is the example way to do my processing. This text is on my Richtextbox
I want to populate content in between word example and word text.
Answer is way to do my processing. This
Please help me doing this using c#.
You could use RegEx or, assuming text is your richtextbox text, this code:
string from = "example";
int iStart = text.IndexOf(from) + from.Length;
int iEnd = text.IndexOf("text", iStart);
string result = text.Substring(iStart, iEnd - iStart);
You can do like this:
string strTest =
"This is the example way to do my processing. This text is on my Richtextbox";
strTest = strTest.Substring(strTest.IndexOf("example") + 8);
strTest = strTest.Substring(0,strTest.IndexOf("text")-1);
I think the problem is to get the text out of the RichTextBox, try:
var textRange = new TextRange(
richTextBox.Document.ContentStart,
richTextBox.Document.ContentEnd);
var text = textRange.Text;
and then...
(see other answers)

Categories