How can I extract a string using C# Substring [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to extract the browser name and Operating system in differant strings from
this string. How can I use substring to extract them from
{ Browser/leavethis (Operating System) Leavethis/Leavethis (Leavethis, Leavethis) Leavethis/Leavethis/Leavethis }

Instead of splitting the useragent, you should use the properties already supplied the Asp.net Framework to detect the browser/os.
Use this
to get the browser name Request.Browser.Browser and browser version Request.Browser.MajorVersion
to get the Os name Request.Browser.Platform and os version Request.UserAgent

A regex such as the following should work, but I suspect #bastos.sergio's answer is the correct way to get what you need.
Regex r = new Regex(#"{\s+(?<Browser>\w+)/[^(]+\((?<OperatingSystem>[^)]+).+");
string browser;
string OS;
var match = r.Match(s);
if (match.Success)
{
browser = match.Groups["Browser"].Value;
OS = match.Groups["OperatingSystem"].Value;
}

Related

hex_md5 implementation [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Is there an identical hex_md5 algorithm in C# to this: http://pajhome.org.uk/crypt/md5/md5.html
I've looked at the RFC 1321, but I wasn't sure if this was a custom implementation based on RFC 1321. I need it to be identical i.e. the value that hex_md5 would return in the JavaScript implementation will be the same in the C# implementation (if one exists).
This is a fairly typical implementation that should match the library's:
string dataToHash = "aaa";
byte[] dataToHashBytes = System.Text.Encoding.ASCII.GetBytes(dataToHash);
using (var md5 = MD5.Create())
{
var hashed = md5.ComputeHash(dataToHashBytes);
Console.WriteLine(BitConverter.ToString(hashed).Replace("-", ""));
}
The C# code produces 47BCE5C74F589F4867DBD57E9CA9F808. This is fairly trivial code, it takes a string, "aaa", converts it to a a byte representation using the ASCII encoding, then hashes it. Finally, we covert it to hexadecimal.
And here is a JSFiddle from the library producing the same thing: http://jsfiddle.net/QC57K/

Regex to shrink Path.Combine Relative to Absolute [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have 2 file paths:
absolute:
/Content/assets2/otherfolder/another/this/
relative:
../../../../assets/img/logo.gif
when I do Path.Combine(absolute, relative) I get:
/Content/assets2/otherfolder/another/this/../../../../assets/img/logo.gif
which works fine, but what I want to get:
/Content/assets/img/logo.gif
I need a Regex or code that removes the "../" with the corresponding folder:
/Content/assets2/otherfolder/another/this/../../../../assets/img/logo.gif
/Content/assets2/otherfolder/another/../../../assets/img/logo.gif
/Content/assets2/otherfolder/../../assets/img/logo.gif
/Content/assets2/../assets/img/logo.gif
finally into:
/Content/assets/img/logo.gif
You don't need a Regex for that. Actually, I think it would be impossible to define in a reliable way in Regex. Instead, use Path.GetFullPath:
string combined = Path.Combine(path1, path2);
string prettyPath = Path.GetFullPath(combined);

Find a specific number in the middle of a string and remove it from list of numbers [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I don't have much code to show but I am trying to remove a substring (individual number) from a longer string is a series of numbers.
Ex: Main String is "11,12,15,16,55,33,88,100,121,155,115"
Need to find number 16 and remove it from the string leaving...
11,12,15,55,33,88,100,121,155,115
They are a list of id's from a database so I can't just change them to strings. Also how do I remove it as if it wasn't there?
string numbers = "11,12,15,16,55,33,88,100,121,155,115";
numbers = string.Join(",", numbers.Split(',').Where(num => num != "16"));
But why don't you use a List<int> instead for database ID's?
In this specific use case I would split the string using "," as the separator, remove the element that matches, then join the elements again.

Splitting string containing spaces in C# Regex [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For example I have this string:
BTW This is a comment "hahaha"
BTW is a comment operator and all statements after it are ignored.
I need to put BTW as 'comment_operator' and 'This is a comment "hahaha"' as 'comment' in a datagridview.
But I can't do it because I used space as a delimiter in my code, so 'This is a comment "hahaha"' will be concatenated too but I need it as it is.
Can someone enlighten me with this? Thanks.
I assume you want the separate 2 parts by the first occurrence of space. You can use the code below:
string text = #"BTW This is a comment ""hahaha""";
string comment_operator = text.Substring(0, text.IndexOf(' '));
string comment = text.Substring(comment_operator.Length + 1);

c# Regular Expression for the text below? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have strings of array.
One of the strings:
myText = "[Date].[Hierarchy].[Year].&[2005].&[Q1].&[Jan]"
The [Date].[Hierarchy].[Year].& part is always the same, but the rest is changing depending on the today date.
So it would be for today like [Date].[Hierarchy].[Year].&[2013].&[Q3].&[Jul], but in the array I also have other strings that like [Date].[Hierarchy].[Year].&[2005].&[Q1].&[Jan].&[20].
The regex should opt those out also.
I am only interested in strings from array like [Date].[Hierarchy].[Year].&[2005].&[Q1].&[Jan]", [Date].[Hierarchy].[Year].&[2005].&[Q1].&[Feb], [Date].[Hierarchy].[Year].&[2005].&[Q1].&[Mar], ....
I hope you get the pattern.
This Regex should do it. The year is currently set to 4 digits, if you only want it to match 2005, replace the \d{4} bit.
^\[Date\]\.\[Hierarchy\]\.\[Year\]\.&\[\d{4}\]\.&\[Q1\]\.&\[[A-Z-a-z]{3}\]$
Here's the result:
[Date].[Hierarchy].[Year].&[2005].&[Q1].&[Jan] // matches
[Date].[Hierarchy].[Year].&[2013].&[Q3].&[Jul] // no match
[Date].[Hierarchy].[Year].&[2005].&[Q1].&[Jan].&[20] // no match
Edit to your comment: Make sure you put an # before the string declaration.
var pattern = new Regex(#"^\[Date\]\.\[Hierarchy\]\.\[Year\]\.&\[\d{4}\]\.&\[Q1\]\.&\[[A-Z-a-z]{3}\]$");
var matches = pattern.IsMatch("[Date].[Hierarchy].[Year].&[2005].&[Q1].&[Jan]");

Categories

Resources