Remove specific entire word from a string - c#

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

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

C# separate the words in a text file

I have a text file with many words separated by ;. It looks like this:
eye;hand;mouth;arms;book
three;head;legs;home
I would like to go through this file and search for the symbol ; and modify the file so that every word is transposed with line break.
Should I read the text file in a string first with,
string path = #"c:\temp\MyTest.txt";
string readText = File.ReadAllText(path);
Then check:
if readText.contains(";");
But I don't know what to do next
string readText = File.ReadAllText(path);
var result = readText.Replace(";", Environment.NewLine);
use
readText.Replace(";",Environment.NewLine)
Did you mean this?
string g = readText.Replace(";", "\n");

Showing a string without a substring

I got a little "problem". I have these two strings (one showing the message, and the other showing the index where "##Documents##" start):
string text = currentNode.Properties["menuEntry"].Value;
string index = text.IndexOf("##Documents##").ToString();
I want to have another string for the message WITHOUT "##Documents##". Any solution to continue from the code up above, or any other solution?
For example: Message: blablabla ##Documents## 123
And what I want to show: blablabla 123
Thanks and sorry for the noobish question.
Just use the Replace() method like this:
string text = currentNode.Properties["menuEntry"].Value;
string message= text.Replace("##Documents##",string.Empty);
Just concatenate the substrings that occur before and after that substring if it is found.
int index = text.IndexOf("##Documents##");
string newText = text;
if(index > -1)
newText = text.SubString(0, index) + text.SubString(index +13);

how do I replace part of text in textbox in 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();

How to split a string with '#' in C#

I tried to split a string wich contains these character #
domicilioSeparado = domicilio.Split(#"#".ToCharArray());
but every time the array contains just one member. I've tried a lot of combinations but anything seems to work, I also tried to replace the string with a blank space and it kinda works - the problem is that it remains a single string.
domicilio = domicilio.Replace(#"#", #" ");
How can I resolve this?
Complete code:
String[] domicilioSeparado;
String domicilio = dbRow["DOMICILIO"].ToString();
domicilioSeparado = domicilio.Split(#"#".ToCharArray());
if (Regex.IsMatch(domicilioSeparado.Last(), #"\d"))
{
String domicilioSinNum = "";
domicilioSinNum = domicilioSeparado[0];
custTable.Rows.Add(counter, dbRow["CUENTA"], nombre,
paterno, materno, domicilioSinNum, domicilioSeparado.Last(), tipoEntidad);
}
If you just want to split a string on a delimiter, in this instance '#', then you can use this:
domicilioSeparado = domicilio.Split("#");
That should give you what you want. Your second attempt simply replaces all the characters '#' in the string with ' ', which doesn't seem to be what you want. Can we see the string you're trying to split? That might help explain why it's not working.
EDIT:
Ok, here's how I think your code should look, give this a shot and let me know how it goes.
List<string> domicilioSeparado = new List<string>();
String domicilio = dbRow["DOMICILIO"].ToString();
domicilioSeparado = domicilio.Split("#");
if (Regex.IsMatch(domicilioSeparado.Last(), #"\d"))
{
String domicilioSinNum = "";
domicilioSinNum = domicilioSeparado[0];
custTable.Rows.Add(counter, dbRow["CUENTA"], nombre,
paterno, materno, domicilioSinNum, domicilioSeparado.Last(), tipoEntidad);
}
Try this:
string[] domicilioSeparado;
domicilioSeparado = domicilio.Split('#');
Some notes:
1 - It is ('#'), instead of ("#"); 2 - Replace does not split a string, it only replace that part, keeping as a single string.
In case you want an example that includes the printing of the whole array:
string domicilio = "abc#def#ghi";
string[] domicilioSeparado;
domicilioSeparado = domicilio.Split('#');
for (int i = 0; i < domicilioSeparado.Length; i++)
{
MessageBox.Show(domicilioSeparado[i]);
}
It will open a Message Box for each element within domicilioSeparado.

Categories