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

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);

Related

Select a part in a string C# [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 5 years ago.
Improve this question
I have string
string AccountName= "123456789 - Savings - 20$"
Now I want to select the Accountname Savings only. That means the part between -s.
That data is dynamic data. So, Could you please give me an idea to get the part of string between two -s. i.e AccountName= "Savings".
Thank you in advance..!!
One of the ways you can to that:
Split string by '-'
Get the position 1 of array
Trim value to remove spaces
var AccountNameSplited= AccountName.split('-')[1].Trim();
You should be defensive in this cases:
var AccountNameBt = AccountName.Split('-');
var AccountNameBtPos1 = string.Empty;
if (AccountNameBt != null && AccountNameBt.Count() > 0)
AccountNameBtPos1 =AccountNameBt[1].Trim();
Assuming your string will always have only two -s, you could using the following to get the substring between them. If this is not the case please modify the question to better describe the issue.
string myString = AccountName.Split('-')[1];
Check out https://msdn.microsoft.com/en-us/library/system.string.split(v=vs.110).aspx for more information on the Split method in the string class.
How about:
string AccountName = "123456789 - Savings - 20$";
String[] tokens = AccountName.Split(new[] { " - " }, StringSplitOptions.RemoveEmptyEntries);
AccountName = tokens.ElementAtOrDefault(1); // Savings
If it's possible that there are no spaces:
String[] tokens = AccountName.Split(new[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
AccountName = tokens.ElementAtOrDefault(1)?.Trim();
Use Regex:
AccountName = Regex.Match(AccountName, #"-\s*(.*?)\s*-").Groups[1].Value;
Demo: https://dotnetfiddle.net/3Xr24T

How can I extract sub-string from string in c# code? [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 6 years ago.
Improve this question
How can I extract this sub-string "60684" from this string "/fa/Viewer/Switcher/60684/0" in c#?
You can use Split method :
string str = "/fa/12012/Switcher/60684/0";
string str2 = str.Split('/')[4];
One way is to use regex:
static void Main()
{
string pattern = #"\/[a-zA-Z]+\/[a-zA-Z]+\/[a-zA-Z]+\/([0-9]+)\/[a-z0-9]+";
var regex = new Regex(pattern);
string path = "/fa/Viewer/Switcher/60684/0";
var match = regex.Match(path);
Console.WriteLine(match.Groups[1].ToString());
}
Like this:
string url = #"/fa/12012/Switcher/60684/0";
string[] NumberAfterSwitcher = url.Split('/');
string num = (NumberAfterSwitcher.Length > 3) ? (String)url.Split('/').GetValue(3) : String.Empty;

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();

Split a string into two parts by using word [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 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];

Extract the first two words from a string with C# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I guys im trying to workout C# code to extract the first two words from string. below is code im doing.
public static string GetDetailsAsString(string Details)
{
string Items = //how to get first 2 word from string???
if (Items == null || Items.Length == 0)
return string.Empty;
else
return Items;
}
Define "words", if you want to get the first two words that are separated by white-spaces you can use String.Split and Enumerable.Take:
string[] words = Details.Split();
var twoWords = words.Take(2);
If you want them as separate string:
string firstWords = twoWords.First();
string secondWord = twoWords.Last();
If you want the first two words as single string you can use String.Join:
string twoWordsTogether = string.Join(" ", twoWords);
Note that this simple approach will replace new-line/tab characters with empty spaces.
Assuming the words are separated by whitespaces:
var WordsArray=Details.Split();
string Items = WordsArray[0] + ' ' + WordsArray[1];

Categories