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);
Related
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]","");
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.
I have the textbox that takes some string. This string could be very long. I want to limit displayed text (for example to 10 characters) and attach 3 dots like:
if text box takes value "To be, or not to be, that is the question:" it display only "To be, or..."
Or
if text box takes value "To be" it display "To be"
Html.DevExpress().TextBox(
tbsettings =>
{
tbsettings.Name = "tbNameEdit";;
tbsettings.Width = 400;
tbsettings.Properties.DisplayFormatString=???
}).Bind(DataBinder.Eval(product, "ReportName")).GetHtml();
You should use a Label control to display the data. Set AutoSize to false and AutoEllipsis to true. There are good reasons why a TextBox does not have this functionality, among which:
where are you going to store the truncated data?
if the user selects the text to edit or even copy, how do you handle that?
If you counter-argument is that the TextBox is read-only, then that is only more reason to re-think the control you are using for this.
If you need to use a regex, you can do this:
Regex.Replace(input, "(?<=^.{10}).*", "...");
This replaces any text after the tenth character with three dots.
The (?<=expr) is a lookbehind. It means that expr must be matched (but not consumed) in order for the rest of the match to be successful. If there are fewer than ten characters in the input, no replacement is performed.
Here is a demo on ideone.
Try this:
string displayValue = !string.IsNullOrWhiteSpace(textBox.Text) && textBox.Text.Length > 10
? textBox.Text.Left(10) + "..."
: textBox.Text;
In an extension method:
public static string Ellipsis(this string str, int TotalWidth, string Ellipsis = "...")
{
string output = "";
if (!string.IsNullOrWhiteSpace(str) && str.Length > TotalWidth)
{
output = output.Left(TotalWidth) + Ellipsis;
}
return output;
}
Using it would be:
string displayValue = textBox.Text.Ellipsis(10);
string textToDisplay = (inputText.Length <= 10)
? inputText
: inputText.Substring(0, 10) + "...";
Something like this?
static void SetTextWithLimit(this TextBox textBox, string text, int limit)
{
if (text.Length > limit)
{
text = text.SubString(0, limit) + "...";
}
textBox.Text = text;
}
Show what you have tried and where you are stuck.
You don't need to use regex
string s = "To be, or not to be, that is the question:";
s = s.Length > 10 ? s.Remove(10, s.Length - 10) + "..." : s;
string maxStringLength = 10;
string displayStr = "A very very long string that you want to shorten";
if (displayStr.Length >= maxStringLength) {
displayStr = displayStr.Substring(0, maxStringLength) + " ...";
}
//displayStr = "A very very long str ..."
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;
I have a string and I want to replace a part of it.
The tricky part is that that I can't use Regex.replace, because I only know the start and end positions of the data in the string.
For example, if the string looks like this:
I love cats, some more stuff here, we dont know how much more
And I have start=8 and end=11. And I want to replace that part to whatever I need to. This time lets say dogs so the new string will look like:
I love dogs, some more stuff here, we dont know how much more
How I could do that?
Simplest way:
string replaced = original.Substring(0, start) + replacementText +
original.Substring(end);
I had expected StringBuilder to have something which would do this, but I think you'd have to call Remove then Insert.
str.Substring(0, 8) + "replacement" + str.Substring(11);
It's not "elegant", but it works.
ReplaceAt(int index, int length, string replace)
Here's an extension method that doesn't use StringBuilder or Substring. This method also allows the replacement string to extend past the length of the source string.
//// str - the source string
//// index- the start location to replace at (0-based)
//// length - the number of characters to be removed before inserting
//// replace - the string that is replacing characters
public static string ReplaceAt(this string str, int index, int length, string replace)
{
return str.Remove(index, Math.Min(length, str.Length - index))
.Insert(index, replace);
}
When using this function, if you want the entire replacement string to replace as many characters as possible, then set length to the length of the replacement string:
"0123456789".ReplaceAt(7, 5, "Salut") = "0123456Salut"
Otherwise, you can specify the amount of characters that will be removed:
"0123456789".ReplaceAt(2, 2, "Salut") = "01Salut456789"
If you specify the length to be 0, then this function acts just like the insert function:
"0123456789".ReplaceAt(4, 0, "Salut") = "0123Salut456789"
I guess this is more efficient since the StringBuilder class need not be initialized and since it uses more basic operations.
Hope this help
string newString =
String.Concat(
originalString.Substring(0, start),
replacementText,
originalString.Substring(end));
OR
StringBuilder sb = new StringBuilder(originalString);
sb
.Remove(start, length)
.Insert(start, replacementText);
Not elegant but funny solution :
string myString = "I love cats, some more stuff here, we dont know how much more";
Regex expr = new Regex("cats");
int start = 8;
int end = 11;
Match m =expr.Match(myString);
if (m.Index == start-1 && m.Length == end - (start-1))
{
Console.WriteLine(expr.Replace(myString, "dogs"));
}
Just for fun with LINQ:
const string S = "I love cats, some more stuff here, we dont know how much more";
const string Dogs = "dogs";
var res = S
.Take(7)
.Concat(Dogs)
.Concat(S.Where((c, i) => i > 10));
var resultString = string.Concat(res);