This question already has answers here:
C# How to split string based on 2 parameters?
(2 answers)
Closed 5 years ago.
I have a string here : Name_20160204_102-10002
I want it to be spited into 4 pieces like
str[0] = "Name";
str[1] = "20160204";
str[2] = "102";
str[3] = "10002";
I'm really poor in regular expression if anyone know how to solve please kindly let me know
Use string.Split() and pass it an array of characters to split on.
"Name_20160204_102-10002".Split(new char[] {'_', '-'});
Which gives the output:
["Name",
"20160204",
"102",
"10002"]
Related
This question already has answers here:
Split string and get Second value only
(5 answers)
Closed 1 year ago.
I would like to split the string below using "|" as separator but would also like to extract only the strings after the colon (:) into the array:
Input:
CompanyID:1234|CompanyName:ABC
Desired Output would be an array with values:
1234, ABC
Current code so far is:
string text = "CompanyID:1234|CompanyName:ABC";
string[] ids = text.Split('|');
Is there a way to do it in one line?
Appreciate anyone's help.
Thanks.
text.Split('|').Select(s => s.Split(':').Last()).ToArray()
This question already has answers here:
C# Regex to allow only alpha numeric
(6 answers)
Closed 5 years ago.
I need to check whether string is only contain letters but not numbers or special characters.I used below regex pattern,
String validText = "^[a-zA-Z-]+$";
its work fine for 'Leo#' but if it is like 'Leo#1' its not working properly.
Anyone have idea ?
I prefer you can use LinQ (input is your test string)
bool result = input.All(Char.IsLetter);
Else as Gordon Posted the right Regex,
^[a-zA-z]+$
You can try using this regex
/^[A-Za-z]+$/
This will match only letters in your string ..
This question already has answers here:
.NET / C# - Convert char[] to string
(8 answers)
Closed 5 years ago.
How do I combine two char's into a single string?
It seems as I cannot for the life of me find out how to do it whatsoever.
char[] chars = {'a', 'b'};
string s = new string(chars);
This question already has answers here:
string.split - by multiple character delimiter
(6 answers)
Closed 6 years ago.
Hey guys I have a quick question. I am trying to parse a single string into multiple strings using a keyword. I can currently find a set of code that allows me to parse the single string but I need to store each of the new strings as a new variable/item.
string full = "Hey//Please//Help";
parse into:
first string- Hey
Second string- Please
third string- Help
So for example I would like to manipulate the first string, Hey, by itself. Please let me know if any more explanation is necessary.
Simple String.Split:
string[] results = full.Split(new string[] { "//" }, StringSplitOptions.RemoveEmptyEntries);
foreach(string str in results)
{
Console.WriteLine(str);
}
Result:
Hey
Please
Help
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
String.Format exception when format string contains “{”
Does following possible using C# String.Format?
Required output "products/item/{itemId}"
I've tried escaping braces but this does not work:
const string itemIdPattern = "itemId";
string result = String.Format("products/item/{{0}}", itemIdPattern);
Preferably something more nice than
string result = String.Format("products/item/{0}{1}{2}",
"{",
itemIdPattern,
"}");
You'll need 3 braces per side for that -- 2 for the braces and 1 for the replacement.
string result = String.Format("products/item/{{{0}}}", itemIdPattern);