I would like to transforme this string '" into this string >>.
I have already read this thread.
Here is what I've tried:
string str = "'"";
str = str.Replace("'\"",">>");
It is not throwing any error but doesn't do anything neither. Thank you very much.
string.Replace returns a new string since strings are immutable in C#.
Simply do str = str.Replace("'\"",">>");
Related
I was trying to remove last part of a string but failed.Here string named D:\software\VS2012\newtext.txt and i want to trim last section of string so here newtext.txt . I should get D:\software\VS2012 but how to do it in c#.When i tried it is removing all the string that has '\'. Here is what i did in c#
string str = #"D:\softwares\VS2012\newtext.txt";
str= str.Remove(str.IndexOf('\\'));
Console.WriteLine(str);
There is a premade function for this in the framework
string str = #"D:\softwares\VS2012\newtext.txt";
string path = System.IO.Path.GetDirectoryName(str);
(Reference)
Note that your original code does not work because you are removing from the first backslash, not the last. Substitute this line to make your code work
str = str.Remove(str.LastIndexOf('\\'));
Try using System.IO.Path.GetDirectoryName(string):
string dirname= System.IO.Path.GetDirectoryName(#"D:\softwares\VS2012\newtext.txt");
For removing a known portion of a string you can simply use the Replace.
In your case:
str = str.Replace("\\newtext.txt", ""); //this will give you the same result of the System.IO.Path.GetDirectoryName already suggested by gmiley, but it's more in a string context as per your question
Though if you want to remove the last part of a string by the last encounterd known character then the suggested "LastIndexOff('\')" method already suggested along with the Remove.
If you want to use a delimiter method, so depending on the delimiter character but not on the string format (in your case path format) the LastIndexOff(char) is the best option.
Although you could also split the string into an array and then rejoin the array after removing the last element:
var delmimter = '\\';
var strAy = str.Split(char);
str = String.Join('\\', strAy.SkipLast(1).ToArray());
With this method you don't need to rely on the existence of the delimiter char in the string and the result is always without the delimiter char at the end.
Besides, you can easily create an extension with the delimiter as a parameter.
We should check the existance of the char also
string str = #"D:\softwares\VS2012\newtext.txt";
int rstr = str.LastIndexOf('\\');
if (rstr>0) str= str.Remove(rstr);
Console.WriteLine(str);
How can I replace the following strings "Assigned_To" into "AssignedTo""Assigned_To_You" into "AssignedToYou" By C#.NET
string str = "Assigned_To_You".Replace("_", string.Empty);
Use
String.Replace("_","");
it will replace every underscore by nothing.
String means every object of string type.
"Assigned_To".replace("_", "");
I am using Visual studio 2010.
I need to replace some part of my string, thus I should find the specific charter in my string.
but .find is not available for me.
What should I do, is there any specific library for that?
string selected="ddddtttjjj";
selected.find("t");
C#, String.IndexOf
string str = "abcdefg";
var ix = str.IndexOf("d");
Console.WriteLine("Ix=" + ix);
// output
//Ix=3
http://msdn.microsoft.com/en-us/library/k8b1470s(v=vs.110).aspx
but, if you want to repleace a substring you can use. "String.Replace"
string str = "abcdefg";
str = str.Replace("d","xDx");
Console.WriteLine(str);
// ouput
// abcxDxefg
http://msdn.microsoft.com/en-us/library/fk49wtc1(v=vs.110).aspx
note that String.Replace Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.
How to get text before a symbol in string ? Any ideas?
e.g. acsbkjb/123kbvh/123jh/
get text before first - "/"
Try this
string ss = myString.Split('/')[0];
You can use Substring() method to get the required part of the string.
String text="acsbkjb/123kbvh/123jh/";
int index=text.IndexOf('/');
String text2="";
if(index>=0)
text2=text.Substring(0,index);
get substring like
youstring.Substring(0,yourstring.IndexOf('/'));
The IEnumerable approach
string str = "acsbkjb/123kbvh/123jh/";
var result = new string(str.TakeWhile(a => a != '/').ToArray());
Console.WriteLine(result);
If there are no forward slashes this works without need to check the return of IndexOf
EDIT Keep this answer just as an example because the efficiency of this approach is really worse. IndexOf works faster also if you add an if statement to check the return value.
string text = "acsbkjb/123kbvh/123jh/";
string text2 = text.Substring(0, text.IndexOf("/"));
If I write something like this:
string s = #"...."......";
it doesn't work.
If I try this:
string s = #"...\".....";
it doesn't work either.
How can I add a " character to a multi line string declaration in C#?
Try this:
string s = #"..."".....";
The double character usage also works with the characters { and } when you're using string.Format and you want to include a literal instance of either rather than indicate a parameter argument, for example:
string jsString = string.Format(
"var jsonUrls = {{firstUrl: '{0}', secondUrl: '{1}'}};",
firstUrl,
secondUrl
);
string s = "...\"....."; should work
the # disables escapes so if you want to use \" then no # symbol
Personally i think you should go with
string s = string.format("{0}\"{1},"something","something else");
it makes it easier in the long run