Passing string value after apply split function in asp.net c# - c#

I have fail to pass the string value to label or text box after split function. So my question is how to store the string value in label after applying the split function.
string strData2 = "samsung,apple,htc";
char[] separator2 = new char[] { ',' };
string[] strSplitArr = strData2.Split(separator2);
foreach (string arrStr in strSplitArr)
{
Response.Write(arrStr + "<br/>");
}
(e.g. label.text = ""+ the split string value )
Thanks

You can use String.Join:
label.Text = String.Join("," , strSplitArr);
Concatenates all the elements of a string array, using the specified
separator between each element.

Try:
label.text = label.text + arrStr;
to list one after other with break, do
label.text = label.text + arrStr + "</br>";

Do you want to join these in one string again? Use String.Join:
label.Text += String.Join(' ',strSplitArr);
source: http://msdn.microsoft.com/pl-pl/library/57a79xd0.aspx

Related

Get values from one textbox and put them in another textboxes

I have values from a textbox :
"r, 00.00m,0000521135Hz,0000000000c,0000000.000s, 025.1C"
and I want to make each value show in another textboxes like this:
textbox 1:
a: "00.00"
textbox 2:
b: "0000521135"
textbox 3:
c: "0000000.000"
textbox 4:
d: "025.1"
I can do this in arduino using parseInt(),
I wonder how to do this in c#, any help?
you can use a string.split() function to extract value from first textbox.
string baseStr = "r, 00.00m,0000521135Hz,0000000000c,0000000.000s, 025.1C";
List<string> colStr= test.Split(new char[','], StringSplitOptions.RemoveEmptyEntries);
and then remove alphabet using regular expression
using System.Text.RegularExpressions;
...
Textbox1.Text = Regex.Replace(colStr[1], "[A-Za-z]", "");
Textbox2.Text = Regex.Replace(colStr[2], "[A-Za-z]", ""));
...
This will give you the idea how to put the data in the textboxes. I have done it for a string variable.
string s1 = "r, 00.00m,0000521135Hz,0000000000c,0000000.000s, 025.1C";
string[] spliteds1 = s1.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
string txt1 = "";
foreach(string elem in spliteds1)
{
if(Regex.Replace(elem, "[^0-9.]", "") != "")
{
txt1 = txt1 + Regex.Replace(elem, "[^0-9.]", "") + ",";
}
}
This code will put the in txt1 with comma seperator. You can run your loop for textboxes.
Hope this helps

RichTextBox not printing newline

I have this method that replaces(in bold) some words in a string and show the changed string in a ritchtextBox.
In the final string I need to replace the # symbol by a newline.
I already tried checked this forum tying several solutions, but nothing worked.
The method I use is
private string bold(string ing)
{
StringBuilder builder = new StringBuilder();
ing = " " + ing + " ";
builder.Append(#"{\rtf1\ansi");
foreach (string word in splitwords)
{
var regex = new Regex(#"(?<![\w])" + word + #"(?![\w])", RegexOptions.IgnoreCase);
ing = regex.Replace(ing, m => #"\b" + m.ToString() + #"\c0");
}
ing = ing.Replace(#"\b", #"\b ");
ing = ing.Replace(#"\c0", #" \b0");
ing = ing.Replace("#", Environment.NewLine);
builder.Append(ing);
builder.Append(#"}");
MessageBox.Show("builder.ToString():" + builder.ToString());
return builder.ToString();
}
When I call the this method and "put it" in the ritchTextBox it doesn´t print the new line
ingred.Rtf = bold(ingd);
How should I solve this??
EDIT:: input string - line1 # line2 # line3
output in the MessageBox
Builder.ToString() : {\rtf1\ansi\b line1\b0
line2
line3
}
output in the ritchTextBox: line1 line2 line3
Instead of
ing = ing.Replace("#", Environment.NewLine);
Try
ing = ing.Replace("#", #"\par\r\n");
Use This Code For Repalce
int startIndex = 0, index;
RichTextBox myRtb = new RichTextBox(); // if have A richtextBox Remove thisline and Use your Richtextbox
myRtb.Rtf = STRRTF;// if have A richtextBox Remove thisline and Use your Richtextbox
while ((index = myRtb.Text.IndexOf("#", startIndex)) != -1)
{
myRtb.Select(index, word.Length);
myRtb.SelectedText ="\n";
startIndex = index + 1;
}
Better use Environment.NewLine for adding new line also Make sure yourRTB.MultiLine property is set to true. assign string to richtext box like this yourRTB.AppendText(t)
Try replacing it with a "\r\n" character sequence?
edit: is MultiLine property of ritchtextBox enabled?

Find Chr(13) in a text and split them in C#

I have written two lines of code below in vb6. The code is :
d = InStr(s, data1, Chr(13), 1) ' Fine 13 keycode(Enter) form a text data.
sSplit2 = Split(g, Chr(32)) ' Split with 13 Keycode(Enter)
But I can't write above code in C#. Please help me out. How can I write the above code in C#.
I believe you are looking for string.Split:
string str = "Test string" + (char)13 + "some other string";
string[] splitted = str.Split((char)13);
Or you can use:
string[] splitted = str.Split('\r');
For the above you will get two strings in your splitted array.
the equivalnt code for sSplit2 = Split(g, Chr(32)) is
string[] sSplit2 = g.Split('\n');
int index = sourceStr.IndexOf((char)13);
String[] splittArr = sourceStr.Split((char)13);
const char CarriageReturn = (char)13;
string testString = "This is a test " + CarriageReturn + " string.";
//find first occurence of CarriageReturn
int index = testString.IndexOf(CarriageReturn);
//split according to CarriageReturn
string[] split = testString.Split(CarriageReturn);
If you want to encapsulate the carriage return depending on whether you are running in a unix or non unix environment you can use Environment.NewLine . See http://msdn.microsoft.com/en-us/library/system.environment.newline(v=vs.100).aspx .
string testString2 = "This is a test " + Environment.NewLine + " string.";
//find first occurence of Environment.NewLine
int index2 = testString2.IndexOf(Environment.NewLine);
//split according to Environment.NewLine
string[] split2 = testString2.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

How to capture exact td value in jquery independent of browser

I have li inside it i contain td.
var values = '';
$("#list4 li.add table .mytd").each(function () {
values = $(this).html() + '|' + values;
});
document.getElementById("MainContent_uscRetailParameters_hdRetailCustomerGroup").value = values;
__doPostBack('<%=txtRCCode.ClientID%>', '');
when I capture in hidden field it come like this
[alot of spaces]CHARMINSENSITIVE-1
with lot of spaces how can i retrieve the exact value in all browser. this space not found in Internet explorer. but in firefox it comes with spaces how could i capture the exact td value.
string lvValues = hdProductGroup.Value;
//string trim = lvValues.Replace(" ", "");
string trim = lvValues.Replace("\r", "");
trim = trim.Replace("\r", "");
trim = trim.Replace("\n", "");
trim = trim.Replace("\t", "");
string str = trim;
string[] list = str.Split('|');
You could trim it in Jquery? Try something like the following:
values = $.trim($(this).html() + '|' + values);
Or at the server using something like:
value = value.Trim();

C# replace part of string but only exact matches possible?

I have a string beginString = "apple|fruitsapple|turnip";
What I want to do is replace just apple with mango, not fruitsapple.
string fixedString = beginString.Replace("apple","mango"); This doesn't work because it replaces both apple and fruitsapple.
Any ideas?
beginString = "|" + beginString + "|";
fixedString = beginString.Replace("|apple|","|mango|");
This cannot be done in the way you have said since it will consider the entire string to be a string. You can do the split by | as you have used or else have the strings in a list and use equals and then replace it.
String[] words = beginString.Split("|");
now do the replace on words. works for any scenario.
The variation on other answers in LINQ style:
string fixedString = string.Join("|",
beginString
.Split('|')
.Select(s => s != "apple" ? s : "mango"));
Closest I can get. Was gonna suggest regular expression, but that won't always work as you want. You have to split the string first and then remake it.
string searchString = "apple";
string newString = "mango";
string beginString = "apple|fruitsapple|turnip";
string[] array = beginString.Split('|');
foreach (var item in array)
{
if (item == searchString)
item.Replace(searchString, newString);
}
string recreated = "";
new List<string>(array).ForEach(e => recreated += e + "|");
recreated.TrimEnd('|');
string newstr = Regex.Replace("apple|fruitsapple|turnip", #"\bapple\b", "mango");

Categories

Resources