How can I extract sub-string from string in c# code? [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 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;

Related

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

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

Replacing string literals in c# using Regex [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 am trying to replace all string literals in a string, with placeholders. For example if I have the following string:
string s1 = "foo"; string s2 = "bar"; string s3 = "baz";
I would like to replace this with:
string s1 = #0#; string s2 = #1#; string s2 = #2#;
and also retain the replaced string literals {"foo","bar", "baz"} in a data structure for later use.
I can do this through brute force ugly coding. However, I am wondering whether there is a nice way of doing this using regular expressions?
My attempt was:
MatchCollection textConstants = Regex.Matches(text, "\".*\"");
for (int i=0; i < textConstants.Count; i++)
{
text=text.Replace(textConstants[i].Value, "#" + i + "#");
}'
This does not seem very nice
And now you have two problems:
var s = "string s1 = \"foo\"; string s2 = \"bar\"; string s3 = \"baz\";";
var list = new List<string>();
var result = Regex.Replace(s, "\".*?\"", m => { list.Add(m.Value);
return "#" + (list.Count - 1) + "#"; });
Take a look at this msdn article. It's a good starting point on how to use regular expressions.
https://msdn.microsoft.com/en-us/library/xwewhkd1(v=vs.110).aspx

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

how can i remove "/ ONLY" from "Rs1500/ONLY",using 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 8 years ago.
Improve this question
string oldstring = textBox9.Text;
string newstring = oldstring.Remove(0, 2);
string o = newstring.Remove(4, 7);
Now, I want to get only "1500",rest of the things to be removed.
How can I do this? Please help me.
Try the following code
string newstring = Regex.Replace(oldstring, #"[^\d]", "");
It shall work.
there are number of ways to do this.. you can use Split() as below:
string oldstring = "Rs1500/ONLY";
string[] newstring = oldstring.Split('/');
string o = newstring[0];
This will give you "RS 1500" as result.
And if you want to remove RS as well then just add the below code in last of the above code:
string final = newstring[0].ToString().Replace("Rs","");
That's All
You can use Replace
string oldstring = textBox9.Text;
string newstring = oldstring.Replace("Rs","").Replace("/ONLY","");
This should give you only 1500
Try this way
string ss = "Rs1500/ONLY";
string[] newss = ss.Split('/');
ss = newss[0].ToString().Replace("Rs","");//Or try string.Empty() for instead of ""
See this demo code : with output

Categories