This question already has answers here:
Why does the Replace() string method not modify my string variable?
(4 answers)
Closed 9 years ago.
string mystring="the are boys";
string[] tags = {"the"};
string[] replace ={"they"}
mystring.Replace(tags[0],replace[0]) // is not working
mystring.Replace("the","they") // is working
I thought both are same but first statement is not working. The second one is.
Please help me to solve the problem.
I assume that you don't assign the return value of String.Replace to the variable. But since strings are immutable you have to do that:
mystring = mystring.Replace(tags[0],replace[0])
Related
This question already has answers here:
How to get the first five character of a String
(22 answers)
Closed 2 years ago.
I have a string like this: first/exemple/import/status/5b12f918-f2ef-436f-808c-50ea48da000
I need to the output like this: first/exemple/import/
I need to remove from status and so on. I thought about remplace but it wont work because this 5b12f918-f2ef-436f-808c-50ea48da000 is variable so it changes.
if there any function can do this please
If you can guarantee the structure of the text (ie that structure won't change):
public static ReturnFirst(string inString){
var path = inString.Split('//')[1];
return $"//{path}//";
}
Or read the first 'x' characters of the string
This question already has answers here:
Regex escape with \ or \\?
(5 answers)
Closed 4 years ago.
I'm getting the not enough )'s error, but as best I can tell I've got all the )'s in.
line getting error:
string pathFriendlyDevId = Regex.Match(device, "VEN.*(?=\\)").ToString().Replace("&", "_");
device string getting parsed:
string device = "PCI\\VEN_144D&DEV_A804&SUBSYS_A801144D&REV_00\\4&10B60712&0&00EA";
the goal here is to get everything from VEN to REV_00.
Am I missing something obvious here?
As was pointed out above, using a verbatim string such as #"VEN.*(?=\\)" did the trick
This question already has answers here:
Convert string to Color in C#
(10 answers)
Closed 4 years ago.
the question probably already has been answered, but i honestly dont know what to search for, an "Reflection" did not solve my issue.
I want to call a field by a string such as:
string str = "Green";
Color colorForPurpose = Color.str;
Of course this does not work, but the purpose should be clear.
Anyone has an idea?
You can use
string str = "Green";
Color colorForPurpose = Color.FromName(str)
See this post for further information.
This question already has answers here:
Check if a string contains one of 10 characters
(6 answers)
Closed 2 years ago.
I have this array:
char[] signOfDecimal = {',', '.'};
and I'm trying to find the location of one of these 2 characters in my string:
locationOfDotInRate = myString.IndexOf(signOfDecimal[0], signOfDecimal[1]);
Basically I'm trying to check where one of these characters might occur in my string, but its not working. The reason I'm doing it this way is because my string sometimes contain '.' or ','.
You can use IndexOfAny method
locationOfDotInRate = myString.IndexOfAny(signOfDecimal);
This question already has answers here:
string.Replace (or other string modification) not working
(4 answers)
Closed 4 years ago.
I have (hopefully) a simple problem.
String.Insert(0, "test");
is not working, yet:
String.Text += "test";
does..? I need Insert so I can insert something in the middle of a string.
I'm such an idiot, instant Google search fixed this.
String.Insert does not modify the string it is called on, but instead returns a new string with the inserted text in.
So I needed:
String = String.Insert(0, "test");
it should work
see the below example
string names = "Romeo Juliet";
string shakespeare = names.Insert(6, "and ");
Console.WriteLine(shakespeare);
the result will be Romeo and Juliet
see the fiddle