Strings with parameters for a method in C# [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 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

Related

how to find in between Ou=9999998 Only number [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
How to find substring (Ou=9999998 only number) from string?
For example, suppose this is my string:
string myString = "cn=54445sddsfsd,ou=9998fgfgfgf8855,o=fgfgfdg,u=dfddfgfgg,subject=5454gffdgfg454‌​hg";
I want to only 99988855
I suggest converting the string to dictionary with a help of Linq:
using System.Collections.Generic; // for Dictionary
using System.Linq; // Linq: ToDictionary
...
String source =
"cn=54445sddsfsd,ou=99988855,o=fgfgfdg,u=dfddfgfgg.subject=5454gffdgfg454hg";
Dictionary<String, String> data = source
.Split(',')
.ToDictionary(line => line.Substring(0, line.IndexOf('=')),
line => line.Substring(line.IndexOf('=') + 1));
then get the value
// "99988855"; you may want to put int.Parse(data["ou"]); if you want integer value
String result = data["ou"];
or check if there's value and get it if it is:
String result;
if (data.TryGetValue("ou", out result)) {
// "ou" key found
}
else {
// no "ou" key found
}
Try this way you will get all the number part from this string after equal to symbol in an arry.
string myString = "cn=54445sddsfsd,ou=99988855,o=fgfgfdg,u=dfddfgfgg,subject=5454gffdgfg454hg";
string[] myArray= myString.Split(',');
string[] finalOutPut = (from item in myArray
where Regex.IsMatch(item.Substring(item.LastIndexOf('=') + 1), #"^\d+$")
select item.ToString()).ToArray<string>();
In this code the array finalOutput will have one value ie) ou=99988855.

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

Replace the particular part of string? [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 want to replace particular string in another string if it contain that word . example
give string is "asp,mvc,c#,wpf" and another string is "<b>asp</b>,<b>wpf</b>" and my final result should be "<b>asp</b>,mvc,c#,<b>wpf</b>" , i have no idea about how to do it in c# code .please help me.
You can do this:
var str = "asp,mvc,c#,wpf";
var anotherStr = "<b>asp</b>,<b>wpf</b>";
var myArr = anotherStr.Replace("<b>", "").Replace("</b>", "").Split(',');
foreach (string value in myArr)
{
str = str.Replace(value, "<b>" + value + "</b>");
}
Console.WriteLine(str);

Replace the same line if contains any word in it 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 9 years ago.
Improve this question
Example:
If there is a line http://google.com/adi/727412;sz=728x90;ord=$RANDOM? which contains adi in it, wants it to be replaced with http://google.com/adi/727412;sz=728x90;click=$CLICK;ord=$RANDOM? and rest all other text to be same with no change.
Please help
This is a fairly simple task:
string url = #"http://google.com/adi/727412;sz=728x90;ord=$RANDOM?";
if (url.Contains(#"/adi/"))
{
int pos = url.IndexOf(";ord"); //// Find first occurence of Ord parameter
url = url.Insert(pos, ";click=$CLICK"); //// Insert text at position
}
Edit: To accomplish the task for multiple occurences I used a solution from this thread.
{
string url = "<google.com/adi/727412;sz=728x90;ord=$RANDOM?>; <google.com/adi/727412;sz=300x250;ord=$RANDOM?>";
string searchString = #"/adi/";
int n = 0;
while ((n = url.IndexOf(searchString, n)) != -1)
{
n += searchString.Length;
int pos = url.IndexOf('?', n);
url = url.Insert(pos, ";click=$CLICK");
}
}
string url = "http://google.com/adi/727412;sz=728x90;ord=$RANDOM?";
if(url.Contains("adi")) url = "http://google.com/adi/727412;sz=728x90;click=$CLICK;ord=$RANDOM?";
string url = "blablablablablahttp://google.com/adi/727412;sz=728x90;ord=$RANDOM?blablabla";
if(url.Contains("adi")) url.Replace("http://google.com/adi/727412;sz=728x90;ord=$RANDOM?", "http://google.com/adi/727412;sz=728x90;click=$CLICK;ord=$RANDOM?");

Categories