My url contains all' + 's like path/My+Property+Details but I need to replace all + with '-'.and make it:
path/My-Property-Details.
Use String.Replace(..), like so:
string s = "path/My+Property+Details";
s = s.Replace("+", "-");
Don't forget the assignment because a string is immutable.
Use String.Replace.
url = url.Replace("+", "-");
Try this:
string newURL = oldUrl.Replace("+", "-");
Related
I have a url like this:
http://ycchoi/sites/dev/Lists/List/AllItems.aspx
And I want to split it like this:
Lists/List/AllItems.aspx
How can I do that?
This might do the trick for you.
string ddr = "http://ycchoi/sites/dev/Lists/List/AllItems.aspx";
string[] ddrs = ddr.Split('/').Skip(Math.Max(0, ddr.Split('/').Count() - 3)).ToArray();
string ExpectedURL = String.Join("/", ddrs);
As per Diligent Key Presser's comment : You can do this using following code...
url = "http://ycchoi/sites/dev/Lists/List/AllItems.aspx";
string newParth = url.Replace("http://ycchoi/sites/dev/" , "");
From a string, check if it starts with a value 'startsWithCorrectId'...if it does remove the value from the start. Problem being if this value is also found again in the string, it will also remove it. I realise this is what .replace does...but is there something like .startsWith to RemoveAtStart?
string startsWithCorrectId = largeIconID.ToString();
//startsWithCorrectId will be '1'
string fullImageName = file.Replace("_thumb", "");
//fullImageName will be "1red-number-1.jpg"
//file will be '1red-number-1_thumb.jpg'
if(file.StartsWith(startsWithCorrectId))
{
fullImageName = fullImageName.Replace(startsWithCorrectId, "");
//so yes this is true but instead of replacing the first instance of '1'..it removes them both
}
Really what I would like is for '1red-number-1.jpg' to become 'red-number-1.jpg'....NOT 'red-number-.jpg'..replacing all instances of 'startsWithCorrectId' I just want to replace the first instance
One solution is to use Regex.Replace():
fullImageName = Regex.Replace(fullImageName, "^" + startsWithCorrectId, "");
This will remove startsWithCorrectId if it's at the start of the string
if(file.StartsWith(startsWithCorrectId))
{
fullImageName = fullImageName.SubString(startsWithCorrectId.Length);
}
if I have undestood your correctly you would need to get a string starting from correctId.Length position
if(fullImageName .StartsWith(startsWithCorrectId))
fullImageName = fullImageName .Substring(startsWithCorrectId.Length);
if you like extensions:
public static class StringExtensions{
public static string RemoveFirstOccuranceIfMatches(this string content, string firstOccuranceValue){
if(content.StartsWith(firstOccuranceValue))
return content.Substring(firstOccuranceValue.Length);
return content;
}
}
//...
fullImageName = fullImageName.RemoveFirstOccuranceIfMatches(startsWithCorrectId);
You can do so with a regular expression where you can encode the requirement that the string starts at the beginning:
var regex = "^" + Regex.Escape(startsWithCorrectId);
// Replace the ID at the start. If it doesn't exist, nothing will change in the string.
fullImageName = Regex.Replace(fullImageName, regex, "");
Another option is to use a substring, instead of a replace operation. You already know that it's at the start of the string, you can just take the substring starting right after it:
fullImageName = fullImageName.Substring(startsWithCorrectId.Length);
I want to replace all the " and ' from a string
eg strings
"23423dfd
"'43535fdgd
""'4353fg
""'''3453ere
the result should be
23423dfd
43535fdgd
4353fg
3453ere
I tried this myString.Replace("'",string.Empty).Replace('"',string.Empty); but its not giving me the correct result.
Use String.Replace
mystring = mystring.Replace("\"", string.Empty).Replace("'", string.Empty)
Do two replaces:
s = s.Replace("'", "").Replace("\"", "");
Try this:
string s = yoursting.Replace("\"", string.Empty).Replace("'", string.Empty);
The below string is coming from a DIV tag. So I have enclosed the value below.
String cLocation = "'target="_blank'></a><img alt='testimage.jpg' src='/SPECIMAGE/testimage.jpg'"
I would like to replace in the above string by changing "src="/" with "src='xyz/files'".
I have tried the typical string.Replace("old","new") but it didn't work.
I tried the below,
cNewLocation ="xyz/files";
cNewString = cLocation.Replce("src='/'", "src='" + cNewLocation + "'/")
It didn't work.
Please suggest.
If I'm understanding what you're asking, you could use Regex to replace the string like so:
var cNewString = Regex.Replace(cLocation, #"src='/.*/", "src='" + newLocation + "/");
EDIT : I modified the regular expression to replace src='/.../ with src='{newLocation}/
you might try looking at the Replace command in c#.
so mystring = srcstring.Replace("old", "New");
http://msdn.microsoft.com/en-us/library/system.string.replace%28v=vs.71%29.aspx
possible replace the / in the string with //?
You can do the following:
string cLocation = "'target='_blank'></a><img alt='testimage.jpg' src='/SPECIMAGE/testimage.jpg'";
cLocation = cLocation.Replace("src='/'", "src='xyz/files'");
This fixes the problem:
int start = cLocation.IndexOf("src='") + 5;
int end = cLocation.LastIndexOf("'");
string xcLocation = cLocation.Remove(start, end - start);
string cLocation = xcLocation.Insert(start , "xyz/files");
I have got a string as follows:
string name ="C:\folder\back-201190082233.zip";
How can I get only the part 201190082233 from the string name? I have tried like this for getting the only the part 201190082233
string filetype = name;
string[] getfiledate = filetype.Split('-');
But I am getting the part 201190082233.zip. Now I want to get only the part 201190082233. Would anyone please help on this?
Seems like a good idea to use regular expressions:
var match = Regex.Match("back.201190082233.zip" , #"(?<=-)\d+(?=\.)");
if(match.Success)
{
var numericPart = match.Value;
}
Edit:
If you're dealing with paths, .Net offers help:
string name = #"C:\folder\back.201190082233.zip";
var fileName = Path.GetFileName(name);
var match = Regex.Match(fileName , #"(?<=-)\d+(?=\.)");
if(match.Success)
{
var numericPart = match.Value;
}
string name = "C:\folder\back-201190082233.zip";
string filetype = name;
string[] getfiledate = filetype.Split(new[] {'.', '-'});
string datepart = getfiledate[1];
How about this way?
var fileDate= filetype.Split('.')[1];
Edit for updates
var fileDate = Path.GetFileNameWithoutExtension(filetype).Split('.')[0]
Probably
var date = Path.GetFileNameWithoutExtension( name ).Split('-')[1];
would be sufficient.
See documentation for function Path.GetFileNameWithoutExtension.
Why are you splitting with a '-' ? Shouldn't it be '.' ?
string numberPart = filetype.Split('.')[1];
You may use something like below
string str = name.Split(".")[1];
Hope this helps!!
or if the string changes you could use a mor specific regular expression like this:
string s = Regex.Replace("back.201190082233.zip", #"[^\.]+\.([^\.]+)\..*", "$1");