This question already has answers here:
Split string using backslash
(3 answers)
Closed 4 years ago.
I'm looking for the best way to extract the computer name from a predictably formatted string. The string will always be in this format:
C:\\Folder1\\Folder2\\NOOBCOMPUTER\\...
If there is a way to extract the contents of a string between the third pair of backslashes and the fourth pair, that should work.
Though I have no idea where to begin with the regex to achieve that, nor do I know if regex is the most "foolproof" way of going about this in C#.
You can split the string and then inspect each element.
string [] s = yourstring.Split("\\");
string final = s[3];
Related
This question already has answers here:
split strings into many strings by newline?
(4 answers)
Closed 2 years ago.
I want to parse this string: "2.8\r\n2.52\r\n" to just get 2.8 and 2.52. How can I go about doing something like this? I tried to using .Split() but I'm not sure what to pass as parameters to get what I am looking for.
String.Split should work fine:
string[] res = "2.8\r\n2.52\r\n".Split("\r\n", StringSplitOptions.RemoveEmptyEntries); // array with {"2.8", "2.52"}
You need to split by Environment.NewLine which in here would be \r\n
var result = "2.8\r\n2.52\r\n".Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
Edit:
Environment.NewLine depends on your environment as you can guess so to be on the safe side you can also use the exact string (\r\n) as a delimiter.
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:
What is the difference between a regular string and a verbatim string?
(6 answers)
Closed 6 years ago.
I am trying to give the file path in C# which contains special/escape characters. I am new to C#. please help me in defining the file path as raw string literals.
Following is path (\t has spl meaning):
IE_DRIVER_PATH = "C:\software\selenium\temp\drivers\64"
In python we use it as follows (using r - to treat it as raw stiring):
IE_DRIVER_PATH = r"C:\software\selenium\temp\drivers\64"
similarly, In java, we use double slashes.
please help me in defining the same in C#. As of now, I am getting the error which says that file does not exist, though the file is present in the folder.
In C#, you can either use double slashes (like in Java), or you can use # instead of r to create a verbatim string literal:
string IE_DRIVER_PATH = #"C:\software\selenium\temp\drivers\64";
This question already has answers here:
C# string replace does not actually replace the value in the string [duplicate]
(3 answers)
Closed 7 years ago.
I want to replace a single comma (,) with (',').
For example:
"text,text,text" with "text','text','text".
I tried
MyText.Replace(',',"','");
but can't get anything working properly.
Any help would be appreciated.
Try:
MyText = MyText.Replace(",","','");
There are two .Replace methods on strings. One for single characters and one for strings.
When you ',' that defines a single character so it goes to the single character version of the method. If you use double quotes then it defines a string so that version of the method is selected.
Docs on the string replace method: https://msdn.microsoft.com/en-us/library/system.string.replace(v=vs.110).aspx
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);