VS 2015, c#.
I have a string...
string str = "Name;IPAddress";
I want to extract just the IPAddress.
I suspect Regex is the best way to do it but I am unsure.
Any assistance greatly appreciated.
You can use Split
string str = "Name;IPAddress";
string[] both = str.Split(';');
string name = both[0];
string ipadd = both[1];
Why do you think Regex is the best way? Do you also want to validate name and IP address?
string sInput = "John;127.0.0.1";
string[] arrNameAndIP = sInput.Split(';');
bool bIsInputValid = false;
if(arrNameAndIP.Length == 2)
{
Regex rgxNamePattern = new Regex("^[A-za-z]+$");
bool bIsNameValid = rgxNamePattern.IsMatch(arrNameAndIP[0]);
IPAddress ipAddress;
bool bIsIPValid = IPAddress.TryParse(arrNameAndIP[1], out ipAddress);
bIsInputValid = bIsNameValid && bIsIPValid;
}
Related
var myPath = HttpContext.Current.Request.Url.AbsolutePath;
// output: myApplication/myFolder/myPage.aspx
var pageName = Path.GetFileName(myPath);
//output: myPage.aspx
I am trying to output "myFolder/myPage.aspx" without the application path.
Is there built-in option to return that or I would need to use regular expression to get what I need?
Thanks
You should be able to make use of HttpContext.Current.Request.Url.Segments and then a simple string concat:
String[] segments = HttpContext.Current.Request.Url.Segments;
string result = segments[1] + segments[2];
or instead of string concat, use: string result = Path.Combine(segments[1],segments[2]);
This should work
public ActionResult oihoi(string ImageName
{
string _FileName = Path.GetFileName(ImageName.FileName);
string folderpath = "UploadedFiles/WebGallery";
string path = Server.MapPath("~/" + folderpath);
string firstsegment = "";
}
I want to trim the machine name from my Hostname so that I can get the server name.
But I'm not able to figure out how.
This is my code:
string machineName = System.Environment.MachineName;
hostinfo = Dns.GetHostEntry(str); //Fetches the name of the system in the Network (SystemName.canon.co.in)
string Original = hostinfo.HostName.ToString();
Now the string contains the data like:
MachineName.ServerName
blah.comp.co.uk
So I want to remove blah. from the string so that what I am left with is comp.co.uk.
Can anyone help me out with it?
try this
string Original = "blah.comp.co.uk";
string[] ss = Original.Split(".".ToCharArray(), 2);
string Result = ss[1];
EDIT:
string Original = "blah.comp.co.uk";
string[] ss = Original.Split(new[] {'.'}, 2);
string Result = ss[1];
I have this string here:
String FileNameOrginal = "lighthouse-126.jpg";
and I am trying to split the string into 2, seperating it with "-"
I have tried the following, but I get a syntax error on the Spilt:
String FileNameOrginal = drProduct["productHTML"].ToString();
string[] strDataArray = FileNameOrginal.Split("-");
Please Help, I do not understand what I am doing wrong.
You just need a character instead of string:
string[] strDataArray = FileNameOrginal.Split('-');
So the issue is that you need an array for an input, like this:
string[] strDataArray = FileNameOrginal.Split(
new string[] { "-" },
StringSplitOptions.None);
Instead of:
string[] strDataArray = FileNameOrginal.Split("-");
Try
string[] strDataArray = FileNameOrginal.Split('-');
string FileNameOrginal = "lighthouse-126.jpg";
string file1 = FileNameOrginal.Split('-')[0];
string file2 = FileNameOrginal.Split('-')[1];
I have a text called
string path = "Default/abc/cde/css/";
I want to compare a text.
string compare = "abc";
I want a result
string result = "Default/abc";
The rest of the path /cde/css is useless.Is it possible to grab the desire result in asp.net c#. Thanks.
Is this what you looking for?:
string result = path.Substring(0, path.IndexOf(compare)+compare.Length);
Try this. This will loop through the different levels (assuming these are directory levels) until it matches the compare, and then exit the loop. This means that if there is a folder called abcd, this won't end the loop.
string path = "Default/abc/cde/css";
string compare = "abc";
string result = string.Empty;
foreach (string lvl in path.Split("/")) {
result += lvl + "/";
if (lvl == compare)
{
break;
}
}
if (result.Length>0)
{
result = result.substring(0, result.length-1);
}
string path = "Default/abc/cde/css/";
string answer = "";
string compare = "abc";
if (path.Contains(compare ))
{
answer = path.Substring(0, path.IndexOf(stringToMatch) + compare.Length);
}
Something like the above should work.
I suggest that if you meet questions of this kind in the future, you should try it yourself first.
string result = path.Contains(compare) ? path.Substring(0, (path.IndexOf(compare) + compare.Length)) : path;
On my forum I have a lot of redundant link data like:
[url:30l7ypk7]http://www.box.net/shared/0p28sf6hib[/url:30l7ypk7]
In regexp how can I change these to the format:
http://www.box.net/shared/0p28sf6hib
string orig = "[url:30l7ypk7]http://www.box.net/shared/0p28sf6hib[/url:30l7ypk7]";
string replace = "$1";
string regex = #"\[url:.*?](.*?)\[/url:.*?]";
string fixedLink = Regex.Replace(orig, regex, replace);
This isn't doing it totally in Regex but will still work...
string oldUrl = "[url:30l7ypk7]http://www.box.net/shared/0p28sf6hib[/url:30l7ypk7]";
Regex regExp = new Regex(#"http://[^\[]*");
var match = regExp.Match(oldUrl);
string newUrl = string.Format("<a href='{0}' rel='nofollow'>{0}</a>", match.Value);
This should capture the string \[([^\]]+)\]([^[]+)\[/\1\] and group it so you can pull out the URL like this:
Regex re = new Regex(#"\[([^\]]+)\]([^[]+)\[/\1\]");
var s = #"[url:30l7ypk7]http://www.box.net/shared/0p28sf6hib[/url:30l7ypk7]";
var replaced = s.Replace(s, string.Format("{0}", re.Match(s).Groups[1].Value));
Console.WriteLine(replaced)
This is just from memory but I will try to check it over when I have more time. Should help get you started.
string matchPattern = #"\[(url\:\w)\](.+?)\[/\1\]";
String replacePattern = #"<a href='$2' rel='nofollow'>$2</a>";
String blogText = ...;
blogText = Regex.Replace(matchPattern, blogText, replacePattern);