How to capture exact td value in jquery independent of browser - c#

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();

Related

Change special chars in string

I am using c# and in code from appsettings.json I take strings and convert them if special chars exists. this is my code
int? a = applicationRequestViewModel.GetApplicantIndex();
int? g = applicationRequestViewModel.GetGurantorIndex();
foreach (var keys in _options.Value.RegisterParamKeys)
{
string value = keys.Split(";")[0];
string name = keys.Split(";")[1];
string key = value.Split(":")[typeOfApplicant];
key = Regex.Replace(key, #"[^\[a\]]", "[" + a + "]");
key = Regex.Replace(key, #"[^\[g\]]", "[" + g + "]");
var registrationProperty = new RegistrationProperty() { };
registrationProperty.Name = name;
registrationProperty.Value = (string)rss.SelectToken(key);
listOfRegistrationProperty.Add(registrationProperty);
}
from appsettings.json I took below strings
"RegisterBatchParams": [
"applicationInfo.applicationNumber:applicationInfo.applicationNumber:applicationInfo.applicationNumber:applicationInfo.applicationNumber;applicationNumber",
"applicationInfo.applicantType:applicationInfo.applicantType:applicationInfo.applicantType:applicationInfo.applicantType;applicantType",
"applicationInfo.customerSegment:applicationInfo.customerSegment:applicationInfo.customerSegment:applicationInfo.customerSegment;customerSegment",
"applicationInfo.applicationStatusLocalText:applicationInfo.applicationStatusLocalText:applicationInfo.applicationStatusLocalText:applicationInfo.applicationStatusLocalText;applicationStatus",
"applicationRequestViewModel.applicants[a].businessPartner.person.firstName:applicationRequestViewModel.applicants[a].businessPartner.person.firstName:applicationRequestViewModel.applicants[a].businessPartner.person.firstName:applicationRequestViewModel.applicants[a].businessPartner.person.firstName;customerName"
],
for the last string I want to change "applicants[a]" to with index number but it doesn't convert as expected how can I convert correctly?
As expected result
applicationRequestViewModel.applicants[0].businessPartner.person.firstName
but given result
a[0][0][0][0][0]a[0][0][0][0][0][0][0][0][0]a[0][0][0][0][0]a[0][0][0][0][0][0][0][0][0][0]
Instead of #"[^\[a\]]" use #"\[a\]".
But you don't even need regex for this. Simple string.Replace will do the job just as well.
Or, you can try this regex and replace only char inside of parentheses.
[a](?=[]])

How to I get the desired part from the string?

I have this string, I want to get the part after the date. The part till date always remains the same. I would have hoped to get the index of date but it changes always hence I can't use it.
var str = "c:\ somefolder\ download\ 2019-14-11 merchandise of today"
char[] spearator = {" "};
var _split = str.Split(spearator);
Here I have all the words broken down according to spaces.
How do I get the 'merchandise of today'?
You can try following codes, use the regular expression
var str = #"c:\ somefolder\ download\ 2019-14-11 merchandise of today";
var reg = new Regex(#".+\d{4}-\d{2}-\d{2}");
var result = reg.Replace(str, string.Empty).Trim();
If the format of the date does not change, you could do the following:
var path = #"C:\Example\Download\2019-14-11 Merchandise Of Today";
var merchandise = path.Substring(path.LastIndexOf('\\')).Trim();
OR
var merchandiseWithoutDate = path.Substring(path.IndexOf(' ').Trim();
That will output the date and merchandise of today, if you change the character to a space you will also retrieve that.
What Substring is basically allows you to control an index to begin and end, which allows you to mitigate the text received.
//Thanks guys!
//This is what I did before I saw the two answers. I know it is messy a lot and now I am //gonna try the substring method but this was a temporary fix for me. Thanks again!
var str = "c:\ somefolder\ download\ 2019-14-11 merchandise of today"
char[] spearator = {" "};
var _split = str.Split(spearator);
int len = _split.lenght-1;
for(i=0; i<_split.lenght; i++)
{
var getStr = _split[i]
Match search = regex.match(getStr, "my regex patern for date");
if (search.success)
{
var converted = search.tostring();
int index = Array.IndexOf(_split, converted)
index++;
string temp = "";
while(index<=len_)
{
string temp = temp+ " "+_split[index];
index++;
}
console.writeline("String is {0}", temp);
}
}

Extract values from html tags in a string?

I have the following strings of HTML tags:
string markup = "<b>this</b><s/><i>is</i><s/><r>text</r>";
string markup2 = "<b>this</b><i>hello</i><i>is</i><r>text</r>";
string markup3 = "<s/><b>this</b><i>hello</i><i>is</i><r>text</r>";
string markup4 = "<b>this</b><i>hello</i><i>is</i><r>text</r><s/>";
string markup5 = "<s/><b>this</b><i>hello</i><i>is</i><r>text</r><s/>";
string markup6 = "<s/><b>this</b><i>hello</i><s/><s/><s/><r>text</r><i>is</i><s/>";
How can I extract the value inside each tag regardless of which string is used, and printing a space instead of the s tag
If the tags are "empty" (only "< s />") you basically replace a string in a string.
e.g.:
string x = markup.Replace("<s/>", " "); //Replaces all occurrences of <s/>
Try this:
var input = "<b>this</b><s/><i>is</i><s/><r>text</r>";
var tmp = input.Replace("<s/>", " "); // <s/> Replace
var final = Regex.Replace(tmp, "<.*?>", string.Empty); // HTML delete

How to to remove characters from numeric value using regex

I have the value 4,59,999/-. My code is
if (Regex.IsMatch(s,#"\b[1-9]\d*(?:,[0-9]+)*/?-?"))
{
string value = Regex.Replace(s, #"[^\d]", " ");
textBox2.Text= value;
}
Output is: 4 59 999, I need it to be 459999 (without "," , "/" , "-" and " ").
How about without regex?
var s = "4,59,999/-";
var array = s.Where(c => char.IsDigit(c)).ToArray();
or shorter
var array = s.Where(char.IsDigit).ToArray();
And you can use this array in a string(Char[]) constructor.
var result = new string(array); // 459999
You don't need regex, you could use:
textBox2.Text = String.Concat(s.Where(Char.IsDigit));
Much better is to use decimal.Parse/TryParse:
string s = "4,59,999/-.";
decimal price;
if (decimal.TryParse(s.Split('/')[0], NumberStyles.Currency, NumberFormatInfo.InvariantInfo, out price))
textBox2.Text = price.ToString("G");
Just replace with empty string.
string value = Regex.Replace(s, #"[^\d]", ""); // See the change in the replace string.
textBox2.Text= value;
Note You don't require the if as the regex replace will work only if there is a match for non digits ([^\d])
Should just be a case of replacing it with an empty string instead of a space?
Regex.Replace(s, #"[^\d]", String.Empty);
Currently you're replacing these characters with a space. Use an empty set of quotes instead.
if (Regex.IsMatch(s,#"\b[1-9]\d*(?:,[0-9]+)*/?-?"))
{
string value = Regex.Replace(s, #"[^\d]", "");
textBox2.Text= value;
}
You are replacing the ",", "/", "-" and " " with a white space. Try this instead:
string value = Regex.Replace(s, #"[^\d]", "");
Hope this helps.
Linq is a possible solution:
String s = "4,59,999/-";
...
textBox2.Text = new String(s.Where(item => item >= '0' && item <= '9').ToArray());
Try something like this it will work 100% in php so just change some syntax for C#
<?php
$str = "4,59,999/-.";
echo $str = preg_replace('/[^a-z0-9]/', '', $str);
?>

Passing string value after apply split function in asp.net 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

Categories

Resources