Spliting string into array based on curly brackets [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
For Example I have String like this:
""dear customer{Customer name} your reference number is {referenceNumber}"
I want to get array=["{Customer name}",{referenceNumber}]"
I have to split based on curly bracket inside bracket value is changeable means it can be different for different cases I just need to split and get array of value inside brackets including brackets.

If you think about it, splitting on { and } will produce an array where every odd index is what you want..
.Split('{','}').Where((s,i)=>i%2==1).Select(s=>'{' + s + '}').ToArray();
Split the string, use the LINQ Where function that passes the int index to the predicate, insist that the index be odd (mod2 is 1) and select a new string that puts the brackets back on, ToArray

Related

Removing the commas from string in c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have this string:
AppleBanana
With C# code, I was managed to bring like this:
Apple,Banana,
But I don't want the last comma in it.
So for that I used:
string obj = myVar.Trim().Trim(','); // this is just to remove the whitespace and trailing comma
But nothing happens. My string is still the same i.e. it shows Apple,Banana,.
Then I tried this:
if (myVar.EndsWith(", "))
myVar= myVar.Remove(myVar.Length - 1); // but this removes all the commas from the string.
Where am I missing?
string s = "Apple,Banana,";
s = s.Remove(s.Length - 1);
It's clear that you're working with comma-delimited data, so treat it as such. Split it into an array. While you're splitting it, you can remove empty entries, which will have the effect of ignoring the final comma.
var list = myVar.Split(',', StringSplitOptions.IgnoreEmptyEntries);
If you need it as a comma-separated string again, join it:
var myVar = string.Join(",", list);
Remove the last character.
string obj = myVar.Remove(myVar.Length - 1);
Update
#JohnWu solution is better, more coherent, more robust.

Save data from a texbox in an array (vector) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have the following question: How could I save what is in a texbox with numbers, separated from each other by commas, which are entered by the user in an array (vector)? The maximum number of numbers that can be entered is 3, and separated by commas, since I need to make a cross product of vectors (Product point). In advance thanks for the help.
You can simply use "split" and it will create the array.
var arrayList = yourTexbox.Text.split(",");
Give textboxes name like txt[1], txt[2], txt[3] and receive that from backend as array which contains value of textbox1 in 1th index , textbox2 in 2nd index etc
Use Regex.Matches with a pattern that only matches sets of 3

Split string on every 500 values with comma [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
how to split this string on every 500 values ending in comma
string val= "1,2,3,4,5,.....................,2000,2001";
//Solution
[1,2,3,...,500]
[501,502,503,...,1000]
[1001,1002,...,1500]
You can match the string with the following regex instead (where 499 is 500 minus 1):
(?:[^,]+,){0,4}[^,]+
Demo (for splitting at every 5 commas here): https://regex101.com/r/nbRxdv/2
Assuming by "no loops" you mean you're happy to let LINQ use loops internally, maybe something like this:
string s = "your,comma,string";
string[] ss = s.Split(',');
Print500(ss, 0);
private void Print500(IEnumerable<string> ies, int skip)
{
if (skip > ies.Count())
return;
Console.Out.WriteLine(string.Join(",", ies.Skip(skip).Take(500)));
Print500(ies, skip + 500);
}
I haven't run it, so it might have some minor issues..

String contains a lot of copies, how to get rid of them? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I got something like this:
string s="Solid;Solid;Gass;Solid;Solid;Gass;Solid;Gass;Liquid;Liquid;"
and now I want to get rid of the copies in the string...so that in the end s should be like this:
s="Solid;Gass;Liquid;"
Try this:
var parts = s.Split(';');
var distinctParts = parts.Distinct();
var newString = string.Join(";", distinctParts);
Where:
Split will give you an array with all the words of your string, taking the specified character as the word separator (; in this case).
Distinct will give your a collection with the unique words of your array.
Finally, Join composes a new string with the unique words, using the specified string (;in this case) as the separator.
You can split the string, then find the distinct instances and join them back in one line:
string s = "Solid;Solid;Gass;Solid;Solid;Gass;Solid;Gass;Liquid;Liquid;";
s = string.Join(";", s.Split(';').Distinct());

Dictionary values come back with slashes '\' removed? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I saved few directory locations to a Dictionary<string, string>,
e.g.
C:\\WINDOWS\\system32\\abc\\123
But the value that gets stored in the dictionary is C:\WINDOWS\system32\abc\123
So when I later compare a value against one in the dictionary it does a comparison like this:
C:\WINDOWS\system32\abc\123
to this
C:\\WINDOWS\\system32\\abc\\123
How can I retain backslashes when storing values in a Dictionary?
Try this:
Dict.Add(key, #"C:\\WINDOWS\\system32\\abc\\123");
\ is an escape character. Adding # makes your string a string literal instead.
EDIT I've reproduced your issue and this fix will solve it.
Use the # symbol in front of the strings when saving. That should solve it.

Categories