I have searched up how to do this. All I got was removing text based on a index point. I would like to have it setup where it uses a string to remove text, instead of choosing the point.
Something along the lines of
string text1 = "Hello World!";
string text2 = "Hello";
string text3;
void RemoveText()
{
text3 = text1.Remove(text2);
Console.WriteLine(text3);
}
Output:
Console: World!
Is there a method that can remove text by using a string? I was looking at the Microsoft Docs, but the problem is that the string wouldn't have a denfinite spot that need to be removed.
text3 = text1.Replace(text2,string.empty);
If you wanted to get part of a user inputed responce, you would doing something along the lines of
string textToRemove = "set background ";
Console.WriteLine("Type 'set background ' then your color.");
string userInput = Console.ReadLine();
string answer = userInput.Replace(textToRemove, string.empty);
SetBackgroundColor(answer);
First what you want to do is set the text you don't care about/need
string textToRemove = "set background ";
Next you are going to tell the user what to input and get the input
Console.WriteLine("Type 'set background ' then your color.");
string userInput = Console.ReadLine();
We would then have to make a new string without the unnessary text
string answer = userInput.Replace(textToRemove, string.empty);
In this case we are using the input to set the background color. So we would write a function for changing the background color with a string argument for the color the user gave.
SetBackgroundColor(answer);
You could also use an If statment or an for/foreach statment to see if the user's text without the unneccasry text is in an array or list containing strings that are allowed to be used.
Related
I've saved icon font character codes in database:
f130, f150, ...
How can I show this codes as Icon?
Button.Text = "\u" + IconEntity.Value; // Error: invalid escape character!
if i use string formating, the button's text did not show icon correctly!
Button.Text = #"\u" + IconEntity.Value; //Button Text = \uf130
When you write strings with an escape charecter, c# treats the following text as a single char. For example, writing the string "\u0041" Will result in the string printing as the single char A.
When you start writing "\u" and then break the string, it'll treat it as a sequence of charecters, rather than a single one.
What you can do, however is create a char variable from it's hex value. You can do it by simply casting an int variable or literal to char.
Try implementing something similar to this:
static void Main(string[] args)
{
var str = "41";
var i = int.Parse(str, NumberStyles.HexNumber);
// Prints the char "A"
Console.WriteLine((char) i);
}
Hello I am new to C Sharp & Windows Forms. I am unable to set the specific string of a multiline TextBox. I have tried below things so far.
textBox1.Lines[1] = "welcome to stackOverflow";
The above code does not give a compile time error but when I saw the result using Debug mode it was not expected.
Then i was also reading this MSDN article but in this there is a new collection created by using stream[] constructor but still the same problem arises.
It should give compiler error because you are trying to assign a string to char here:
textBox1.Text[1] = "welcome to stackOverflow";
Text property is of type string, when you use indexer on a string it gives you the char at that position. And also string is immutable so you can't really change a character at specific position without creating a new string.
You should set the Text directly like this:
textBox1.Text = "welcome to stackOverflow";
Or if you have more than one line in an array of string you should set the Lines property:
var lines = new [] { "foo", "bar" };
textBox1.Lines = lines;
Any value that you set directly to textBox1.Lines will be effected to textBox1.
There is a solution to resolve your problem. I think it's best way.
You have to clone the current value of your textbox. Then you set new value on it. Finally, you set back to textbox.
var curValue = (string[])textBox1.Lines.Clone();
curValue[1] = "welcome to stackOverflow";
//Set back to textBox1
textBox1.Lines = curValue;
I am currently making myself a little tool.
Essentially I have this list which goes like this:
NPWR00160_00 LittleBigPlanet
NPWR00163_00 Warhawk
NPWR00179_00 Pure
NPWR00180_00 Midnight Club: Los Angeles
NPWR00181_00 echochrome™
NPWR00187_00 WipEout® HD
This is currently typed into a richTextBox.
I am trying to do this, get the NPWRXXXXXX of the line and save it as a string, and then the Games Name and save that as another string for which I can go ahead and do what I was originially going to do with it. But for each line of the richTextBox which carries on with that formation as above.
Not too sure how to get a line from the richTextBox and save it as a string, in which I can repeat that process for every single line of the richTextBox.
For what I have tried, I gave myself an example that the string was NPWR02727_00 Skydive: Proximity Flight. What I did was this:
string game = "NPWR02727_00 Skydive: Proximity Flight";
string NPWR = game.Substring(0,13);
string gamename = game.Remove(0, 13);
richTextBox2.AppendText("NPWRID: " + NPWR + " Game: " + gamename + Environment.NewLine);
Which actually does successfully save the strings and write it in the second text box as the new form.
Only issue is I'm not sure how to convert a line from the RichTextBox and turn it into a string, and repeat the process for each line in the rich text box
EDIT
So I found out how to turn a string into a line from the richTextBox..
string line = richTextBox1.Lines[0];
So this will get the first line and save it as the string "line"
This now updates the code to
string game = richTextBox1.Lines[0];
string NPWR = game.Substring(0,13);
string gamename = game.Remove(0, 13);
richTextBox2.AppendText("NPWRID: " + NPWR + " Game: " + gamename + Environment.NewLine);
Now how do I get this code to run for every line, I understand I need something to repeat it, and something to change the 0 to count up by 1 everytime it repeats?
EDIT AGAIN
Awesome, forget the above edit, thanks a lot!
I suggest you do something similar to this:
var codes = new List<string>();
var games = new List<string>();
foreach(var s in richTextBox1.Lines)
{
string[] p = s.Split(new char[] { ' ' }, 2);
if (p.Count() == 1) { continue; }
codes.Add(p[0]);
games.Add(p[1]);
}
Basically, we are declaring two Lists of the type string, to store respectively the code and the name of the game. Then we proceed in looping through the Lines property of the RichTextBox, and for each line, we split the line by the first index(space) we find; asking for a maximum of two strings, to avoid splitting any forthcoming elements; in case the name of the game contains spaces.
For the two substrings obtained, we proceed by saving the first part into the List codes, and the second one into the List games.
For further uses(traversing codes/names) we could access the two Lists
for(int c = 0; c < codes.Count; c++)
{
MessageBox.Show(codes[c] + string.Empty + games[c]);
}
I has a string like TPHSFL100 and TPHFL50. For the first string, i want get FL100, for second string i want get HFL50. How I can do this? I do not know which text user will key in, any dynamic solution for this or what algorithm I should use?
string text = "ABCDEF";
string sub = text.Substring(text.Length-5,text.Length);
Console.WriteLine("Substring: {0}", sub);
In my ASP.NET page, I have a string (returned from SQL db). I would like to bold certain part of the string text based on given text position.
For example, if I have a string as follows:
"This is an example to show where to bold the text"
And I am given character start position: 6 and end position: 7, then I would bold the word "is" in my string, getting:
"This is an example to show where to bold the text"
Any ideas?
EDIT: Keep in mind I need to use the start/end position as there may be duplicate words in the string.
Insert a close tag into position 7 of the string
Insert an open tag into position 5 (6 - 1) of the string.
You will get a string like "This is an example…"
I.e. modify string (insert markup) from end to start:
var result = str.Insert(7, "</b>").Insert(6 - 1, "<b>");
You can use String.Replace method for this.
Returns a new string in which all occurrences of a specified string in
the current instance are replaced with another specified string.
string s = "This is an example to show where to bold the text".Replace(" is ", " <b>is</b> ");
Console.WriteLine(s);
Here is a DEMO.
Since you clear what you want, you can use StringBuilder class.
string s = "This is an example to show where to bold the text";
var sb = new StringBuilder(s);
sb.Remove(5, 2);
sb.Insert(5, "<b>is</b>");
Console.WriteLine(s);
Here is a DEMO.
NOTE: Since you didn't see <b> tags as an output, it doesn't mean they are not there ;)
You need to do something like this...
**
strStart = MID(str, 0 , 7) ' Where 7 is the START position
str2Replace = "<b>" & MID(str, 8, 10) & "</b>" ' GRAB the part of string you want to replace
str_remain = MId(str, 11, Len(str)) ' Remaining string
Response.write(strStart & str2Replace & str_remain )
**
First find the string to replace in your full string.
Replace the string with <b>+replacestring+</b>
string str="This is an example to show where to bold the text";
string replaceString="string to replace"
str=str.Replace(replaceString,<b>+replaceString+</b>);
Edit 1
string replaceString=str.Substring(6,2);
str=str.Replace(replaceString,<b>+replaceString+</b>);
SubString Example:
http://www.dotnetperls.com/substring
Edit 2
int startPosition=6;
int lastPosition=7;
int lastIndex=lastPosition-startPosition+1;
string str="This is an example to show where to bold the text";
string replaceString=str.Substring(startPosition,lastIndex);
str=str.Replace(replaceString,<b>+replaceString+</b>);