Split a string into two parts by using word [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 8 years ago.
Improve this question
I would like to split directory into two parts:
For example
//Hello//Products//App//Images//Room//40//Tulips.jpg
into
//Hello//Products//App
and
//Images//Room..40//Tulips.jpg

var splitOn = "App";
var path = "//Hello//Products//App//Images//Room//40//Tulips.jpg";
var parts = path.Split(new string[] { splitOn }, StringSplitOptions.None);
Console.WriteLine(parts[0] + splitOn);
Console.WriteLine(parts[1]);
In order to split by a word (or in this case folder) you need to wrap the term in a string array before passing it to the String.Split function. Splitting on "App" will also remove "App" from the result, so we concatenate it again before we write it to the console.

First split the string based on the double forward slash and then assign to array.
string path= Hello//Products//App//Images//Room//40//Tulips.jpg
string[] names = path.Split('//');
After this collect the words like this:-
string first_part=names[0] + "//" + names[1];

Related

how to remove last string element unnecessary text if exists? [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 2 years ago.
Improve this question
I have a string array with 3 elements like below for example.
string[] stringarray1;
stringarray1 = new string[5]{ “Element 1\n”, “Element 2\n”, “Element 3\n”, “Element 4\n”, “Element 5\nblablablabla” };
Here i need to check last element in string array having unnecessary dynamic text "\nblablablabla", if exists i need to remove(till last of the dynamic text) and replace with "Element 5\n" in last element.
How can I do this?
try this:
for (int i = 0; i < stringarray1.Length; i++)
{
stringarray1[i] = stringarray1[i].Split("\n")[0] + "\n";
}
You can split string with respect to "\n" and take first of it and add "\n" again. This will remove unnessesary characters from your string that i understand.
Instead of checking and replacing unnecessary text, you can replace what ever string you have with expected string
var lastIndex = stringarray1.Length -1;
stringarray1[lastIndex] = $"Element {lastIndex}\n";

How do I split a string with '$' delimiter? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have this FINAL PAYMENT $25 string on a MVC c# application.
I want to split into FINAL PAYMENT and 25
I tried doing this
string s = "FINAL PAYMENT $25";
string[] str1 = s.Split('$');
//result: 25
How can I get the rest. Can anyone help?
Split method returns a string array, if you need both elements of this array, Try:
string s = "FINAL PAYMENT $25";
string[] resArray = s.Split('$');
var FPayment = resArray[0];
var second25= resArray[1];
You can use indexOf instead of a Split
string s = "FINAL PAYMENT $25";
int index = s.IndexOf("$");
String final_pay = s.Substring(index + 1);

I need to ignore a specific line while reading a file [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 need to ignore reading the particular line while reading the whole document.
for example, I have chunk of data and I have read it using File.ReadAllText(filePath); and I need to ignore reading a particular line, Say 50 and need to read the other lines. So far I have the below code.
string fileName = "TextFile.config";
string filePath = Path.GetDirectoryName("TextFile.config") + fileName;
string text = File.ReadAllText(filePath);
You can use ReadLines and Where like here:
int[] ignoreLines = { 50 };
IEnumerable<string> relevantLines = File.ReadLines(filePath)
.Where((line, index) => !ignoreLines.Contains(index + 1));
string resultString = string.Join(Environment.NewLine, relevantLines);
Use File.ReadAllLines, this will give you all lines of the file in an array, you can then loop through this array to check for the line you want to ignore (or not ignore), (either with an index or with string.StartsWith / string.EndsWith
File.ReadLines Method (String)
Reads the lines of a file.
List.RemoveAt Method (Int32)
Removes the element at the specified index of the List.
List.RemoveRange Method (Int32, Int32)
Removes a range of elements from the List.
Exmaple
string fileName = "TextFile.config";
string filePath = Path.GetDirectoryName("TextFile.config") + fileName;
var lines = File.ReadLines(filePath).ToList();
lines.RemoveAt(49) // Remove 50th line
// or
lines.RemoveRange(49,10) // Remove 50th line + 9 more

Strings with parameters for a method 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 6 years ago.
Improve this question
My sentence is: !doar 12345, rayantt
Using string.Contains(!doar), I need get the other values as:
int mount = 12345;
string destiny = "rayannt";
Let the input be the input string as you stated in the question, searchString be the string that you wanted to search for; strParam and intParam are the two required outputs; Now consider the following code:
string input = "!doar 12345, rayantt";
string searchString = "!doar";
string strParam=string.Empty ;
int intParam=0;
if (input.Contains(searchString)) // check for the existence of the search string in given string
{
input = input.Replace(searchString, ""); // remove the searchstring from the input
string[] contents = input.Split(',');
int.TryParse(contents[0], out intParam); // collect the integer param
strParam=contents[1]; // collect the string param
}
// here you get 12345 in intParam and "rayantt" in strParam

Changing Host Names dynamically [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 7 years ago.
Improve this question
I have files on a local server with the address of \\localServerAddress\Folder\Program.exe. I need to remove the server address dynamically and replace it with a different server address that is being selected elsewhere in the form. The server names can be different lengths, therefore, I can not use the string.Substring function.
So given the input
\\localServerAddress\Folder\Program.exe
I would like the result
\\differentServerAddress\Folder\Program.exe
If you are always working with UNCs
Then
string toRemove = new Uri(yourString).host;
string newString = yourString.Replace(String.format(#"\\{0})",toRemove)
, String.format(#"\\{0})",whateveryouwant));
Use this method:
string changeServerInPathString(string originalString, string newServer)
{
List<string> stringParts = originalString.TrimStart('\\').Split('\\').ToList();
stringParts.RemoveAt(0);
stringParts.Insert(0, newServer);
return string.Join("\\", stringParts.ToArray()).Insert(0, "\\\\");
}
You can use something like this:
void Main()
{
string path = #"\\localServerAddress\Folder\Program.exe";
UriBuilder bld = new UriBuilder(path);
bld.Host = "NewServer";
Console.WriteLine(bld.Uri.LocalPath);
}
Result: \\newserver\Folder\Program.exe
string text = #"\\test\FolderName\foo.exe";
text = text.Replace('\\', '-'); \\ this is done as I was not able to make the regex **\\\\\\(.)*?\\** , work.
Regex rg = new Regex("--.*?-"); \\ if in case the above mentioned regex is made to work correctly please replace the regex with the same.
text = rg.Replace(text, "");
Console.WriteLine(text.Replace('-', '\\'));
Console.Read();

Categories