Split string (path of Uri) based on "/" - c#

Wonder if someone could point me in the right direction. What I'd like to achieve is to split a string based upon it having a '/' in it. For example if I had: www.site.com/course/123456/216 in code (c#) I'd like to be able to split the string in code so that 123456 could be assigned to variable param1 and 216 be assigned to param2 (course is the 'friendly' name of the page). If I was to add a third '/' on the string I'd like this to become param3, etc, etc.
Ideally I'd like to be able to put this code somewhere that I could include it on whichever usercontrols I'd need it to work.

Uri.Segments maybe what you are looking for:
new Uri("http://www.contoso.com/foo/bar/index.htm#search").Segments
Results in [ "/", "foo/", "bar/", "index.html" ]

Why not just use split?
var valueArray = "www.site.com/course/123456/216".Split('/');
The array will have the entire string broken up
index 0 would be "www.site.com" and so forth.

HttpContext.Current.Request.Url.AbsolutePath.Split('/')

Well, making the assumption that the values would not have / in them:
var splitVals = queryString.Split('/');
var vals = new Dictionary<string, string>();
for (int i = 2; i <= splitVals.Count; i++)
{
vals.Add(string.Format("param{0}", i), vals[i]);
}
That would get you started. Now, if you're looking to set them to real variables then you'd need to do some casting and leverage reflection, but your question isn't near clear enough to make any real assumptions there.
EDIT
To make this code reusable I would build an extension method:
namespace System
{
public static class StringExtensions
{
public static Dictionary<string, string> SplitQueryString(this string queryString)
{
var splitVals = queryString.Split('/');
var vals = new Dictionary<string, string>();
for (int i = 2; i <= splitVals.Count; i++)
{
vals.Add(string.Format("param{0}", i), vals[i]);
}
return vals;
}
}
}
because then you could do this:
var vals = queryString.SplitQueryString();

http://msdn.microsoft.com/en-us/library/b873y76a.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1
string QueryString = "1234/567/890";
string[] QueryArray = QueryString.Split('/');
Now QueryArray[0] = 1234, QueryArray[1] = 567, QueryArray[2] = 890,

url: server/func2/SubFunc2
// Get path components. Trailing separators. Returns { "/", "func2/", "sunFunc2" }.
string[] pathsegments = myUri.Segments;

Related

How to Convert big IF condition to small

I have to implement a very big If and else statement in a short and smarter way.
for example :
If(seg.status =="hl"||seg.status =="hl2"||seg.status =="hl3"||seg.status =="hl4"||seg.status =="hl4"||seg.status =="hl5"||seg.status =="hl6"||seg.status =="hl7")
into small and smarter way
How about adding all your hl strings in a List<string> and check with Any and Contains like;
var list = new List<string>(){"hl", "hl1", "hl2", ...};
if(!list.Any(seg.status.Contains))
As Jon warned, this will return true if your string doesn't match with status, choose
if(list.Contains(seg.status))
instead.
I would put them into a HashSet:
var keywords = new HashSet<String>(StringComparer.InvariantCultureIgnoreCase)
{
"h1", "hl2", ...
}
And then check if it contains the given word:
if(keywords.Contains(seg.status))
{
// ToDo: What shall happen?
}
You could also use regex:
Regex.IsMatch(seg.status, #"hl\d*")
You can try like this
var segList = new List<string>(){"hl", "hl1", "hl2","hl4", "hl15", "hl6","hl7"};
If(segList.contains(seg.status))
Do you do this in many places? If so, you could write a little string extension class to make the code more tidy:
public static class StringExt
{
public static bool MatchesAnyOf(this string text, params string[] targets)
{
return targets.Any(target => string.Compare(text, target, StringComparison.OrdinalIgnoreCase) == 0);
}
}
And you would call it like this:
if (seg.status.MatchesAnyOf("hl", "hl1", "hl2","hl4", "hl15", "hl6","hl7"))
(If you want a case-sensitive comparison, remove the StringComparison.OrdinalIgnoreCase parameter from string.Compare().)
If you are confident on the pattern of the test variables naming:
using System.Text.RegularExpressions;
Regex re = new Regex("hl[1|2|3|4|5|6|7]");
string[] test= {"hl1","hl2","hl3","hl4","hl5","hl6","hl7","hlasdfasdf"};
for(int i = 0; i < test.Length; i++){
System.Console.WriteLine(test[i] + ":" + (re.IsMatch(test[i])?"match":"does not match"));
}
So in your case:
if(re.IsMatch(seg.status)){ //... }

Extracting data from plain text string

I am trying to process a report from a system which gives me the following code
000=[GEN] OK {Q=1 M=1 B=002 I=3e5e65656-e5dd-45678-b785-a05656569e}
I need to extract the values between the curly brackets {} and save them in to variables. I assume I will need to do this using regex or similar? I've really no idea where to start!! I'm using c# asp.net 4.
I need the following variables
param1 = 000
param2 = GEN
param3 = OK
param4 = 1 //Q
param5 = 1 //M
param6 = 002 //B
param7 = 3e5e65656-e5dd-45678-b785-a05656569e //I
I will name the params based on what they actually mean. Can anyone please help me here? I have tried to split based on spaces, but I get the other garbage with it!
Thanks for any pointers/help!
If the format is pretty constant, you can use .NET string processing methods to pull out the values, something along the lines of
string line =
"000=[GEN] OK {Q=1 M=1 B=002 I=3e5e65656-e5dd-45678-b785-a05656569e}";
int start = line.IndexOf('{');
int end = line.IndexOf('}');
string variablePart = line.Substring(start + 1, end - start);
string[] variables = variablePart.Split(' ');
foreach (string variable in variables)
{
string[] parts = variable.Split('=');
// parts[0] holds the variable name, parts[1] holds the value
}
Wrote this off the top of my head, so there may be an off-by-one error somewhere. Also, it would be advisable to add error checking e.g. to make sure the input string has both a { and a }.
I would suggest a regular expression for this type of work.
var objRegex = new System.Text.RegularExpressions.Regex(#"^(\d+)=\[([A-Z]+)\] ([A-Z]+) \{Q=(\d+) M=(\d+) B=(\d+) I=([a-z0-9\-]+)\}$");
var objMatch = objRegex.Match("000=[GEN] OK {Q=1 M=1 B=002 I=3e5e65656-e5dd-45678-b785-a05656569e}");
if (objMatch.Success)
{
Console.WriteLine(objMatch.Groups[1].ToString());
Console.WriteLine(objMatch.Groups[2].ToString());
Console.WriteLine(objMatch.Groups[3].ToString());
Console.WriteLine(objMatch.Groups[4].ToString());
Console.WriteLine(objMatch.Groups[5].ToString());
Console.WriteLine(objMatch.Groups[6].ToString());
Console.WriteLine(objMatch.Groups[7].ToString());
}
I've just tested this out and it works well for me.
Use a regular expression.
Quick and dirty attempt:
(?<ID1>[0-9]*)=\[(?<GEN>[a-zA-Z]*)\] OK {Q=(?<Q>[0-9]*) M=(?<M>[0-9]*) B=(?<B>[0-9]*) I=(?<I>[a-zA-Z0-9\-]*)}
This will generate named groups called ID1, GEN, Q, M, B and I.
Check out the MSDN docs for details on using Regular Expressions in C#.
You can use Regex Hero for quick C# regex testing.
You can use String.Split
string[] parts = s.Split(new string[] {"=[", "] ", " {Q=", " M=", " B=", " I=", "}"},
StringSplitOptions.None);
This solution breaks up your report code into segments and stores the desired values into an array.
The regular expression matches one report code segment at a time and stores the appropriate values in the "Parsed Report Code Array".
As your example implied, the first two code segments are treated differently than the ones after that. I made the assumption that it is always the first two segments that are processed differently.
private static string[] ParseReportCode(string reportCode) {
const int FIRST_VALUE_ONLY_SEGMENT = 3;
const int GRP_SEGMENT_NAME = 1;
const int GRP_SEGMENT_VALUE = 2;
Regex reportCodeSegmentPattern = new Regex(#"\s*([^\}\{=\s]+)(?:=\[?([^\s\]\}]+)\]?)?");
Match matchReportCodeSegment = reportCodeSegmentPattern.Match(reportCode);
List<string> parsedCodeSegmentElements = new List<string>();
int segmentCount = 0;
while (matchReportCodeSegment.Success) {
if (++segmentCount < FIRST_VALUE_ONLY_SEGMENT) {
string segmentName = matchReportCodeSegment.Groups[GRP_SEGMENT_NAME].Value;
parsedCodeSegmentElements.Add(segmentName);
}
string segmentValue = matchReportCodeSegment.Groups[GRP_SEGMENT_VALUE].Value;
if (segmentValue.Length > 0) parsedCodeSegmentElements.Add(segmentValue);
matchReportCodeSegment = matchReportCodeSegment.NextMatch();
}
return parsedCodeSegmentElements.ToArray();
}

String manuplation in C#

Is there any method that I can use that returns a fixed length array after spliting a string with some delimiter and fill the rest with a default string.
Eg.
string fullName = "Jhon Doe";
string[] names = fullName.SpecialSplit(some parameters); //This should always return string array of length 3 with the second elememnt set to empty if there is no middle name.
Next time specify the language you're asking about. We're no guessers.
In Java:
fullName.split(" ");
And anyway, no method will "return string array of length 3 with the second elememnt set to empty if there is no middle name". For the method, there are just two elements. You have to write that method yourself wrapping the standard split() method.
You should read over Jon Skeet's Writing the perfect question. It will be beneficial to you in the future when posting questions of StackOverflow.
There is no method in C# to do what you are asking, but you can easily write an extension method to do what I think you are asking.
here is a quick example:
public static class AbreviatorExtention
{
public static string[] GetInitials(this String str, char splitChar)
{
string[] initialArray = new string[3];
var nameArray = str.Split(new char[] { splitChar },
StringSplitOptions.RemoveEmptyEntries);
if (nameArray.Length == 2)
{
var charArrayFirstName = nameArray[0].ToCharArray();
var charArrayLastName = nameArray[1].ToCharArray();
initialArray[0] = charArrayFirstName[0].ToString().ToUpper();
initialArray[1] = string.Empty;
initialArray[2] = charArrayLastName[0].ToString().ToUpper();
}
else
{
for (int i = 0; i < nameArray.Length; i++)
{
initialArray[i] = (nameArray[i].ToCharArray())[1]
.ToString().ToUpper();
}
}
return initialArray;
}
}
class Program
{
static void Main(string[] args)
{
string FullName = "john doe";
//Extension method in use
string[] names = FullName.GetInitials(' ');
foreach (var item in names)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
Output:
J
D
I would set it up to split the string separate from the fixed array. If you still want a fixed array, then you set up the array to a size of three an populate. This is not the best method, however, as it has no meaning. Better, set up a person or user class and then populate, via rules, from the split string.

Removing duplicate substrings within a string in C#

How can I remove duplicate substrings within a string? so for instance if I have a string like smith:rodgers:someone:smith:white then how can I get a new string that has the extra smith removed like smith:rodgers:someone:white. Also I'd like to keep the colons even though they are duplicated.
many thanks
string input = "smith:rodgers:someone:smith:white";
string output = string.Join(":", input.Split(':').Distinct().ToArray());
Of course this code assumes that you're only looking for duplicate "field" values. That won't remove "smithsmith" in the following string:
"smith:rodgers:someone:smithsmith:white"
It would be possible to write an algorithm to do that, but quite difficult to make it efficient...
Something like this:
string withoutDuplicates = String.Join(":", myString.Split(':').Distinct().ToArray());
Assuming the format of that string:
var theString = "smith:rodgers:someone:smith:white";
var subStrings = theString.Split(new char[] { ':' });
var uniqueEntries = new List<string>();
foreach(var item in subStrings)
{
if (!uniqueEntries.Contains(item))
{
uniqueEntries.Add(item);
}
}
var uniquifiedStringBuilder = new StringBuilder();
foreach(var item in uniqueEntries)
{
uniquifiedStringBuilder.AppendFormat("{0}:", item);
}
var uniqueString = uniquifiedStringBuilder.ToString().Substring(0, uniquifiedStringBuilder.Length - 1);
Is rather long-winded but shows the process to get from one to the other.
not sure why you want to keep the duplicate colons. if you are expecting the output to be "smith:rodgers:someone::white" try this code:
public static string RemoveDuplicates(string input)
{
string output = string.Empty;
System.Collections.Specialized.StringCollection unique = new System.Collections.Specialized.StringCollection();
string[] parts = input.Split(':');
foreach (string part in parts)
{
output += ":";
if (!unique.Contains(part))
{
unique.Add(part);
output += part;
}
}
output = output.Substring(1);
return output;
}
ofcourse i've not checked for null input, but i'm sure u'll do it ;)

How to provide custom string placeholder for string format

I have a string
string str ="Enter {0} patient name";
I am using string.format to format it.
String.Format(str, "Hello");
Now if i want patient also to be retrieved from some config then I need to change str to something like
"Enter {0} {1} name". So it will replace the {1} with second value. The problem is that I want instead of {1} some other format something like {pat}. But when I try to use, it throws an error. The reason I want a different format is that there are lot of files I need to change like this(which may contain {0},{1} etc). So I need a custom placeholder which can be replaced at run-time.
You might want to check out FormatWith 2.0 by James Newton-King. It allows you to use property names as formatting tokens such as this:
var user = new User()
{
Name = "Olle Wobbla",
Age = 25
};
Console.WriteLine("Your name is {Name} and your age is {Age}".FormatWith(user));
You can also use it with anonymous types.
UPDATE: There is also a similar solution by Scott Hanselman but it is implemented as a set of extension methods on Object instead of String.
UPDATE 2012: You can get Calrius Consulting's NETFx String.FormatWith Extension Method NuGet package on NuGet.org
UPDATE 2014: There is also StringFormat.NET and littlebit's StringFormat
Regex with a MatchEvaluator seems a good option:
static readonly Regex re = new Regex(#"\{([^\}]+)\}", RegexOptions.Compiled);
static void Main()
{
string input = "this {foo} is now {bar}.";
StringDictionary fields = new StringDictionary();
fields.Add("foo", "code");
fields.Add("bar", "working");
string output = re.Replace(input, delegate (Match match) {
return fields[match.Groups[1].Value];
});
Console.WriteLine(output); // "this code is now working."
}
object[] myInts = new int[] {8,9};
However you can get away with:
object[] myInts = new string[] { "8", "9" };
string bar = string.Format("{0} {1}", myInts);
I saw all the answers above, yet, couldn't get the question right :)
Is there any particular reason why the following code does not meet your requirement?
string myFirstStr = GetMyFirstStrFromSomewhere();
string mySecondStr = GetMySecondStrFromSomewhere();
string result = "Enter " + myFirstStr + " " + mySecondStr + " name";
Here's another version of this that I found here: http://www.reddit.com/r/programming/comments/bodml/beef_up_params_in_c_5_to_solve_lambda_abuse/c0nrsf1
Any solution to this is going to involve reflection, which is less than ideal, but here's his code with some of the other major performance issues resolved. (No error checking. Add it if you like.):
1) Uses direct runtime reflection, no DataBinder overhead
2) Doesn't use regular expressions, uses a single-pass parse and state.
3) Doesn't convert the string into an intermediate string and then convert it again to the final format.
4) Allocates and concatenates with a single StringBuilder instead of newing up strings all over the place and concatenating them into new strings.
5) Removes the stack overhead of calling a delegate for n replace operations.
6) In general is a single pass through that will scale in a relatively linear manner (still some cost for each prop lookup and nested prop lookup, but that's that.)
public static string FormatWith(this string format, object source)
{
StringBuilder sbResult = new StringBuilder(format.Length);
StringBuilder sbCurrentTerm = new StringBuilder();
char[] formatChars = format.ToCharArray();
bool inTerm = false;
object currentPropValue = source;
for (int i = 0; i < format.Length; i++)
{
if (formatChars[i] == '{')
inTerm = true;
else if (formatChars[i] == '}')
{
PropertyInfo pi = currentPropValue.GetType().GetProperty(sbCurrentTerm.ToString());
sbResult.Append((string)(pi.PropertyType.GetMethod("ToString", new Type[]{}).Invoke(pi.GetValue(currentPropValue, null), null)));
sbCurrentTerm.Clear();
inTerm = false;
currentPropValue = source;
}
else if (inTerm)
{
if (formatChars[i] == '.')
{
PropertyInfo pi = currentPropValue.GetType().GetProperty(sbCurrentTerm.ToString());
currentPropValue = pi.GetValue(source, null);
sbCurrentTerm.Clear();
}
else
sbCurrentTerm.Append(formatChars[i]);
}
else
sbResult.Append(formatChars[i]);
}
return sbResult.ToString();
}
You can also use the example from Marc Gravell and Extend the String class object:
public static class StringExtension
{
static readonly Regex re = new Regex(#"\{([^\}]+)\}", RegexOptions.Compiled);
public static string FormatPlaceholder(this string str, Dictionary<string, string> fields)
{
if (fields == null)
return str;
return re.Replace(str, delegate(Match match)
{
return fields[match.Groups[1].Value];
});
}
}
Example usage:
String str = "I bought a {color} car";
Dictionary<string, string> fields = new Dictionary<string, string>();
fields.Add("color", "blue");
str.FormatPlaceholder(fields));
You are probably better off using Replace for the custom field and Format for the rest, like:
string str = "Enter {0} {pat} name";
String.Format(str.Replace("{pat}", "Patient"), "Hello");
var user = new User()
{
Name = "John",
Age = 21
};
String result = $"Your name is {user.Name} and your age is {user.Age}";
I wanted something that worked more like Python's string formatting, so I wrote this:
https://gist.github.com/samwyse/b225b32ae1aea6fb27ad9c966b9ca90b
Use it like this:
Dim template = New FormatFromDictionary("{cat} vs {dog}")
Dim d = New Dictionary(Of String, Object) From {
{"cat", "Felix"}, {"dog", "Rex"}}
Console.WriteLine(template.Replace(d)) ' Felix vs Rex

Categories