String.Insert not working [duplicate] - c#

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

Related

How to remove all chars after a space in Textbox [duplicate]

This question already has answers here:
Removing everything after the first " " in a string? (space)
(4 answers)
Closed 9 months ago.
My Problem is that my Users sometimes did mistakes and i want to avoid this by removing the splitted part of the string automatically.
i dont want to combine the splitted string, i just want to remove all what is written after the space.
Should look like this:
All what i selected should be automatically removed after i press the Button to Save/Confirm.
Source: Removing everything after the first " " in a string? (space)
It would be simpler to do a string.Split():
string input = "some:kind of string that I want totrim please.";
input = input.Split()[0];
//input = "some:kind" now
So it should look something like this, based on what are you working on (I suppose WPF)
string inputText = txtInput.Text;
inputText.Split()[0];

How to remove a specific character, From...To the End of string in c# [duplicate]

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

Using a string as a name of an attribute of a class in C# [duplicate]

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.

Replacing ' " ' in a string [duplicate]

This question already has answers here:
How to replace straight quotation mark (")
(3 answers)
Closed 6 years ago.
Some string variables have a " in the data which recks with my php code. I want to take it out in my C# code before the data gets passed to php. I just can't remember the way of doing it, but I tried with this :
line.Replace(#""", "");
This is not correct in Xamarin, what is the good syntax?
I guess this would work.
line.replace("\"","");
use \ to escape it.
line.Replace("\"", "");

string Replace not working in asp.net [duplicate]

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

Categories