There is a code part in my program. Lets say buf.Substring(0, 4) is a string which is 326 for that moment in the loop. String buf.Substring(0, 4) is updated in a for loop.
if (buf.Substring(0, 4).Equals("GG:"))
{
label22.Text = buf.Substring(4) + "Z" ;
}
This outputs on label22 as:
326
Z
If you replace it as
label22.Text = "Z" + buf.Substring(4);
then it concatenates properly as:
Z326
But I want the output to be:
326Z
Whatever I tried it dodn't work. I tried concatenating different ways or sizing label width very long ect. What could be the problem here?
You could try trimming the output of buf.Substring(4) like this
String output = buf.Substring(4).Trim(new char[] {'\r','\n'});
Or just plain Trim() like so
String output = buf.Substring(4).Trim();
Related
If I copy " BOB3 27QK DEPM PJ7J T25G SJZI CJA5 BO5O|123456 " and I want to pass it to my text box, and get only the last 6 digits number in my text box, How to do in c#?
Using .Split would look like this:
string myString = "BOB3 27QK DEPM PJ7J T25G SJZI CJA5 BO5O|123456";
char[] mySplitChars = { '|' };
string[] myArray = myString.Split(mySplitChars);
Console.WriteLine(myArray[1]);
Using .Substring would look like this:
Console.WriteLine(myString.Substring(myString.Length - 6));
The latter is probably preferred because it is shorter and it does not rely on the "|" character being present. The former would be preferred if the "|" is always present but the number of characters at the end can change.
How are you passing it your text box? If it's just getting the last n characters:
refer to this answer
For example, let's say I have this:
string test = "id:test THISISTHESTRINGIWANT id2:test"
And I want to get THISISTHESTRINGIWANT. In order to do that I was wondering if its possible to find it like this:
left_string = "id:test ", right_string = " id2"
Thank You
You can use the .split like this
string test = "id:test THISISTHESTRINGIWANT id2:test"
char[] delimiterChars = { ' '};
string[] output= test.Split(delimiterChars);
this will give you 3 strings {id:test,THISISTHESTRINGIWANT,id2:test}
you can then iterate through the string array with whatever condition you want to weed out the unwanted data or you can change the char array to include any characters you want to split on.
I have written a code to add Suffix at end of each line of a multi-line String but code only appends at the end of string. I am beginner. Can somebody help me in clarifying where I am mistaken? Here is my code:
protected void Prefix_Suffix_Btn_Click(object sender, EventArgs e)
{
String txt_input = Input_id.InnerText.ToString().Trim();
String txt_suffix = Suffix_id.InnerText.ToString().Trim();
String txt_output = Output_id.InnerText.ToString().Trim();
txt_input = txt_input.Replace(txt_suffix + "\n", "\n");
txt_input = txt_input + txt_suffix;
Output_id.InnerText = txt_input;
}
Input:
Line1
Line2
Line3
Desired output:
Line1AppededText
Line2AppendedText
Line3AppendedText
Let's Split text to lines, append each line and, finally, Join into string back:
string source = string.Join(Environment.NewLine,
"Line1",
"Line2",
"Line3");
// Let's have a look at the initial string;
Console.WriteLine(source);
Console.WriteLine();
string result = string.Join(Environment.NewLine, source
.Split(new string[] { Environment.NewLine }, StringSplitOptions.None)
.Select(line => line + "AppendedText"));
Console.Write(result);
Outcome:
Line1
Line2
Line3
Line1AppendedText
Line2AppendedText
Line3AppendedText
The string of that comes out of your Input_id.InnerText is a string that consists of many lines. So if you want to append to each line, you need think of a way to treat those lines separately.
A line-end is denoted as the character '\n'. It looks like 2 characters to you, but the engine will treat it as one: line-end.
What you can do is split (break up) this string into multiple strings by snapping the string whenever you find a '\n'. You can do this by the following:
var lines = Input_id.InnerText.ToString().Split('\n');
Now lines contains an array of strings, each item in there containing a line of the input.
Now you could create a new string that will be built up by your split array as follows:
var newString = "";
foreach(var line in lines) {
newString += line + "<appendText>\n"; //note how we add the \n again since those disappeared by splitting
}
Now newString will contain the new string with each line containing the appended text.
A way shorter answer would be to for instance use the replace function like this:
var newString = Input_id.InnerText.ToString().Replace("\n", "<AppendedText>\n");
There is many ways to do what you want.
You just made a mistake when passing your values into the Replace() method. The documentation for String.Replace() defines it like this:
public string Replace (string oldValue, string newValue);
The first argument ("oldValue") should be the thing you want to replace. The second argument ("newValue") should be the thing you want to change it to. You've just got them the wrong way round. You're asking it to replace the new text (suffix and newline) with the old text (just the newline), which clearly it can't do because the suffix text doesn't exist in the string yet - and it wouldn't be logical even if it worked.
Change
txt_input = txt_input.Replace(txt_suffix + "\n", "\n");
to
txt_input = txt_input.Replace("\n", txt_suffix + "\n");
and you should be fine. As other answers alluded to, there may be nicer ways of achieving the same output, but in terms of fixing your original code this is all you should need to do.
Here's a live demo (just using console output instead of HTML elements): https://dotnetfiddle.net/jnzgUy
I have the following lines of text:
W&BL 15&384&320&214&1&S235JR&&&&&&&&&&S&&0.267&&&&4&&
N&214.nc
A&214&1&&15
W&BL 15&384&320&215&1&S235JR&&&&&&&&&&S&&0.267&&&&4&&
N&215.nc
A&213&2&&14
I want to remove the linebreaks so the outcome will be like this:
A&213&2&&14W&BL 15&384&320&214&1&S235JR&&&&&&&&&&S&&0.267&&&&4&&N&214.nc
A&214&1&&15W&BL 15&384&320&215&1&S235JR&&&&&&&&&&S&&0.267&&&&4&&N&215.nc
I do this because I need to format these lines and I'm putting the whole textfile in a reader per line. When I filter this with linebreaks I can't properly search through the lines. Since I need to delete everything after the S235JR, replace the & with ; and start the line with the BL code.
If someone knows a smarter/better solution to filter these lines, you will be my hero of the day.
Edit for clarification:
This is a example and how it needs to be formatted:
H&HEA100&1712&&1001&2&S235JR&&&HEA100 - 1712&&&&&&&S&&0.96&&&&2&&&1.7&0.2&0.2
N&1001.nc
W&BL 15&384&320&215&1&S235JR&&&&&&&&&&S&&0.267&&&&4&&
N&215.ncA&214&1&&15
H&L80X8&375&&1010&1&S275JR&&&L80X8 - 375&&&&&&&S&&0.117&&&&4&&&0.4&0.1&0.1
N&1010.nc
After formatting:
H;HEA100;1712;;1001;2;S235JR;
BL 15;384;320;215;1;S235JR;
L80X8;375;;1010;1;S275JR;
The input is a text file imported with a StreamReader. The H, BL 15 and L80X8 are determined after 6 & characters. The program was originally written in DOS and I need to convert it into C#. I'm sorry for the confusion.
x.Replace(Environment.NewLine, String.Empty);
where x is string.
From the example in your question, it looks like you want to remove line breaks, but keep every third line break.
You can use a regular expression that matches three lines, and remove the two line breaks between them:
text = Regex.Replace(text, #"(.+)\r\n(.+)\r\n(.+)", "$1$2$3");
x = x.Replace("\r\n", "");
x is your string Object;
string x = x.Replace("&", ";");
string x1 = x.Substring(x.IndexOf("H;"), x.IndexOf("S235JR;", x.IndexOf("H;")) - x.IndexOf("H;")+7);
string x2 = x.Substring(x.IndexOf("BL 15;"), x.IndexOf("S235JR;", x.IndexOf("BL 15;")) - x.IndexOf("BL 15;")+7);
string x3 = x.Substring(x.IndexOf("L80X8;"), x.IndexOf("S275JR;", x.IndexOf("L80X8;")) - x.IndexOf("L80X8;")+7);
string result = x1 + "\r\n" + x2 + "\r\n" + x3;
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;