Splitting a string from a specific point in C# - c#

I have looked at the split string methods in C#, such as:
string[] lines = Regex.Split(value, "\");
Although I have come across the situation where I need to extract a file name from a file path, so I dont want to have to split the string on all occurrences of "\" e.g.:
C:\Windows\System32\calc.exe
Expected output:
calc.exe

new FileInfo(#"C:\Windows\System32\calc.exe").Name

Let the framework take the strain. Use the Path.GetFilename method.
http://msdn.microsoft.com/en-us/library/system.io.path.getfilename.aspx

Related

Extract second section of string

I will have always an string like this:
"/FirstWord/ImportantWord/ThirdWord"
How can I extract the ImportantWord? Words can contain at most one space and they are separated by forward slashlike I put above, for example:
"/Folder/Second Folder/Content"
"/Main folder/Important/Other Content"
I always want to get the second word(Second Folder and Important considering above examples)
how about this:
string ImportantWord = path.Split('/')[2]; // Index 2 will give the required word
I hope you need not to use the String.Split option either with specific characters or with some regular expressions. Since the inputs are well qualified paths to a directory you can use Directory.GetParent method of the System.IO.Directory class, which will give you the parent Directory as DirectoryInfo. From that you can take the Name of Directory which will be the required text.
You can use like this :
string pathFirst = "/Folder/Second Folder/Content";
string pathSecond = "/Main folder/Important/Other Content";
string reqWord1 = Directory.GetParent(pathFirst ).Name; // will give you Second Folder
string reqWord2 = Directory.GetParent(pathSecond).Name; // will give you Important
Additional note: The method Directory.GetParent can be nested if you need to get a name in another level.
Also you may try this:
var stringValue = "/FirstWord/ImportantWord/ThirdWord";
var item = stringValue.Split('/').Skip(2).First(); //item: ImportantWord
There are several ways to solve this. The simplest one is using String.split
Char delimiter = '/';
String[] substrings = value.Split(delimiter);
String secondWord = substrings[1];
(you may want to do some input check to make sure the input is in the right format or else you will get some exception)
Other way is using regex when the pattern is simple /
If you are sure this is a path you can use other answer mention here

How to extract string from java properties

Here is the java properties content
xxx_error_tx1 = This is xxxx. Johe say:
xxx_error_MapCode = xxx_error_tx1, test this function,Failed,\
Default, Current,\
App_Error_tx1
I need to extract string ID and string content, I can extract line1 content correctly, but the second line content extract only the first string xxx_error_tx1, test this function,Failed,\. The rest of string cannot extract.
The regex string is (?<ID>.+?)=(?<Translation>.+?)$, I know this regex have some problem, but I've tried to modify to correct pattern but maybe I am newbie, the result still cannot meet my request.
Any help would be appreciated.
Seems like you want something like this,
(?<ID>.+?)=(?<Translation>(?:(?!\S+\s*=)[\s\S])+)
DEMO
(?:(?!\S+\s*=)[\s\S])+ Matches one or more space or non-space characters which won't contain the string which was matched by this \S+\s*= pattern.
Try this, it correctly include the whole value when the value is splited on multiple lines but stop before line that follow.
(?<ID>.+?)=(?<Translation>(?:.*\\\s)*.*)
DEMO

How to remove a pattern from a string using Regex

I want to find paths from a string and remove them, e.g.:
string1 = "'c:\a\b\c'!MyUDF(param1, param2,..) + 'c:\a\b\c'!MyUDF(param3, param4,..)..."`
I'd like a regex to find the pattern '[some path]'!MyUDF, and remove '[path]'.
Thanks.
Edit:
Example input:
string1 = "'c:\a\b\c'!MyUDF(param1, param2,..) + 'c:\a\b\c'!MyUDF(param3, param4,..)";
Expected output: "MyUDF(param1, param2,...) + MyUDF(param3, param4,...)"
where MyUDF is a function name, so it consists of only letters
input=Regex.Replace(input,"'[^']+'(?=!MyUDF)","");
In case if the path is followed by ! and some other word you can use
input=Regex.Replace(input,#"'[^']+'(?=!\w+)","");
Alright, if the ! is always in the string as you suggest, this Regex !(.*)?\( will get you what you want. Here is a Regex 101 to prove it.
To use it, you might do something like this:
var result = Regex.Replace(myString, #"!(.*)?\(");
The feature you want, if you are dealing with file paths, is in System.Path.
There are many methods there, but that is one of it's specific purposes.

"Evaluate" a c# string

I am reading a C# source file.
When I encounter a string, I want to get it's value.
For instance, in the following example:
public class MyClass
{
public MyClass()
{
string fileName = "C:\\Temp\\A Weird\"FileName";
}
}
I would like to retrieve
C:\Temp\A Weird"FileName
Is there an existing procedure to do that?
Coding a solution with all the possible cases should be quite tricky (#, escape sequences. ...).
I am convinced such procedure exists...
I would like to have the dual function too (to inject a string into a C# source file)
Thanks in advance.
Philippe
P.S:
I gave an example with a filename, but I look for a solution working for all kinds of strings.
I'm pretty sure you can use CodeDOM to read a C# code file and parse its elements. It generates a code tree, and then you can look for nodes representing strings.
http://www.codeproject.com/Articles/2502/C-CodeDOM-parser
Other CodeDom parsers:
http://www.codeproject.com/Articles/14383/An-Expression-Parser-for-CodeDom
NRefactory: https://github.com/icsharpcode/NRefactory and http://www.codeproject.com/Articles/408663/Using-NRefactory-for-analyzing-Csharp-code
There is a way of extracting these strings using a regular expression:
("(\\"|[^"])*")
This particular one works on your simple example and gives the filename (complete with leading and trailing quote characters); whether it would work on more complex ones I can't easily tell unfortunately.
For clarity, (\\"|[^"]) matches any character apart from ", except where it has a leading \ character.
Just use ".*" Regex to match all string values, then remove trailing inverted commas and unescape it.
this will allow \" and "" characters inside your string
so both "C:\\Temp\\A Weird\"FileName" and "Hello ""World""" will match

How do I strip quotes and commandline arguments out from a file path in C#?

I have filepaths in quotes with empty spaced foldernames like this with at least two command line parameters of different length and value appended:
"C:\My Program\Sample Folder\SubFolder2\MyApp1.exe" -parameter13XY1 -parameter101XZ2
I would like to have only the filename without quotes and without commandline arguments:
C:\My Program\Sample Folder\SubFolder2\MyApp1.exe
I would search functions like "StripQuotes" and "StripCommandLineArgs" in the framework, but I didn't find anything similar, since the framework has almost everything missing what would be needed.
As for quotes it would maybe do a "Replace", but the commandline arguments can't be sorted out that way. The filename also contains spaces, so it is not possible to work with Split() as it would cut a part of the filename out.
In the end I would like to have only the filename without quotes and commandline arguments. Note that the filename can also contain empty spaces and hyphens and both combinations of it, like this:
"C:\My Program - Win64\Sample-Folder\Sub -Folder3\My App55- .exe -parameter1 -parameter2 para3"
I have no idea how to find out only the valid path in such cases. There could be also five commandline arguments attached or even 10.
You can use a regular expression "^\"([^\"]*)\".*$" to get the content of the quoted string, like this:
var s = "\"C:\\My Program\\Sample Folder\\SubFolder2\\MyApp1.exe\" -parameter13XY1 -parameter101XZ2";
var res = Regex.Replace(s, "^\"([^\"]*)\".*$", "$1");
Console.WriteLine(res);
Here is a link to ideone.
A slightly heavier answer is to use a command line parsing library like http://commandline.codeplex.com/. It handles all the parsing for you without needing to deal with regular expressions.
String.Split can take a parameter for the character to split on - so if you try this:
var array = filename.Split('"');
You should get back an array with two elements:
array[0] = "C:\My Program\Sample Folder\SubFolder2\MyApp1.exe"
array[1] = "-parameter13XY1 -parameter101XZ2"
The very rough & quick solution:
string cmd = "\"C:\\My Program\\Sample Folder\\SubFolder2\\MyApp1.exe\" -parameter13XY1 -parameter101XZ2";
var allStrings = cmd.Split(new char[] {'"'} );

Categories