So, basically what i need is to get numbers that are between second and third dot.
Example:
I type in my textbox "1.1.1.1" or "183.312.21.132", I click a button and in the seconds textbox I get numbers that are between second and third dot. Like for first one it would be "1" and for seconds one it will be "21"
Sorry for bad English. Thanks!
try split
"1.1.1.1".Split('.')[2]
or
"183.312.21.132".Split('.')[2]
returns a string[] and index 2 would be the third number
Use string split:
"183.312.21.132".Split(".")[index_of_the_dot_before_desired_numbers]
i.e.
"183.312.21.132".Split('.')[2] = "21"
UPD:
if you need a range between dots, you can use LINQ:
var startDotIndex=1;
var endDotIndex=3;
"183.312.21.132".Split('.').Skip(startDotIndex).Take(endDotIndex-startDotIndex).ToArray()
will return ["312", "21"];
string digits[] = "1.2.3.4".Split(".");
Use elsewhere with:
digits[0]
digits[1]
It sounds like you need the String object's Split method see below:
string foo = "183.312.21.132";
string[] foos = foo.Split('.');
from here you can do many different things such as loop through your array and grab values or if you know exactly what index you are looking for you can simply request it straight from the array such as:
string bar = foo.Split('.')[2]; // gives you "21"
var foo = "192.168.0.1";
var digs = foo.Split(".");
var nums = int.Parse(digs[2]);
Related
I have a string which is basically a url something like APIPAth/resources/customers/SSNNumber/authorizations/contracts.
The SSNNumber can be of any value. Its the actual SSN number which I want to remove from the string and the string should look like APIPAth/resources/customers/authorizations/contracts.
I can't find a proper solution in which without hardcoding the word and removing the string
I tried using Find and Replace but I think the function would require the particular word
Looking at the URL you provided, it appears you only want to get rid of digits. You could accomplish that with this line:
var output = Regex.Replace(input, #"[\d]", string.Empty);
There are many ways to skin this cat depending on what stays static.
One way would be to split the url by separators and join them back
var url = #"APIPAth/resources/customers/SSNNumber/authorizations/contracts";
var items = new List<string>(url.Split('/'));
items.RemoveAt(3);
url = string.Join("/", items);
Another way would be to use Regex
var url = #"APIPAth/resources/customers/SSNNumber/authorizations/contracts";
url = Regex.Replace(url, #"/customers/[^/]+/authorizations/", "/customers/authorizations/")
If you elaborate on what you expect in a generic solution, i.e. what part stays static, then I can help you out better
If SSN in number then it has to have form of this 000 00 0000 means 9 digits consequents.
Took a string parse it by / and you get an array of elements lets say parsed
for(int i=0; i<parsed.length; i++){
if(parsed[i].length === 9){
...keep this i...
}
}
remove this parsed[i] from whole parsed and concat with /
How do i split a value in model class.
in list[0].key = 1 and list[0].value = 1_5
here is the code:
if (data.Things.Count != 0)
{
var ans = new List<QuestionModel>();
ans = data.Things.Select(item =>
{
return new QuestionModel()
{
QuestionId = Convert.ToInt32(item.Key),
AnswerId = Convert.ToInt32(item.Value),
};
}).ToList();
}
here i want to split the value.i.e. in AnswerId want only 5.
If you always want the part after the underscore, and it's always an underscore in the format of item.Value, you can do this:
item.Value.Split('_')[1];
This splits the string on the _ and then takes the second part (e.g. what is after the _).
So the complete line of code would be:
AnswerId = Convert.ToInt32(item.Value.Split('_')[1]),
I would add that the fact you are having to do something this clunky is perhaps a symptom of your model not being a good fit to your domain - if you are able to refactor your model such that you don't have to do this and the field in question contains only the data you are interested in then your solution would be cleaner and more easily maintainable.
Come on. Value is a string value right? Did you look at string class what methods does it have? There is a method called split. Read more here: https://msdn.microsoft.com/en-US/library/b873y76a(v=vs.110).aspx
In short:
string value = "1_5";
string[] arr = value.Split('_');
//now arr[0] is "1" and arr[1] is "5"
int i = Convert.ToInt(arr[1]);
I'm not entirely sure, what you're trying to achieve, but I think you want to split the string "1_5" and retrieve just the "5". This can be achieved using string.Split():
(item.Value.Split('_'))[1]
I'm not sure if I worded that right but heres what I'm looking for.
I would like to do something like this:
string lastWord = words.Split(':')[splitResult.Length -1];
Is there any way to make that happen or must I store the array first?
using Linq, LastOrDefault extention.
string lastword = words.Split(':').LastOrDefault();
If I would use Split, wouldnt I be splitting it twice?
It Depends.
if you do below, yes you are splitting twice.
string lastWord = words.Split(':')[words.Split(':').Length -1];
and if you use temporary variable for splits then you need Split only once.
var splits =words.Split(':');
string lastWord = splits[splits.Length -1];
I must doing something wrong... But I can't figure it out!
I have an array with string in it. I'm trying to fins if the Array contains some words like Sales for example.
drillDownUniqueNameArray[0] = "[Sales Territory].[Sales Territories].[Sales Territory Group].&[North America]";//Inside the string array there is this string in index 0
drillDownUniqueNameArray.Contains("S")//Output false!
Array.IndexOf(drillDownUniqueNameArray,"S")//Output -1! <--Fixed My answer
drillDownUniqueNameArray.Contains("[Sales Territory].[Sales Territories].[Sales Territory Group].&[North America]") //Output true!
I thouhgt Contains should find even part of the string..
How can I find if this array have "S" or "Sales" for example?
You are asking if the array contains a string that exactly matches "S".
What you want is to ask if any of the strings in the array contains the character "S", something like:
drillDownUniqueNameArray.Any(v => v.Contains("S"))
You're checking if the array contains an element that's exactly "S" but I think you are trying to check whether the array contains an alement that contains an "S".
You could achieve this by the following statement:
drillDownUniqueNameArray.Any( str => str.Contains ("S") )
You can try this.
drillDownUniqueNameArray[0].Contains("s");
You can use LINQ:
var allWithSales = drillDownUniqueNameArray
.Where(str => str.Contains("Sales"));
ignoring the case:
var allWithSalesIgnoreCase = drillDownUniqueNameArray
.Where(str => str.IndexOf("sales", StringComparison.OrdinalIgnoreCase) >= 0);
If you want to find all that contain a word "Sales"(String.Split() = white-space delimiter)):
var allWordsWithSales = drillDownUniqueNameArray
.Where(str => str.Split().Contains("Sales", StringComparer.OrdinalIgnoreCase));
Now you can enumerate the query with foreach or use ToArray() or ToList to create a collection:
foreach(string str in allWithSales)
Console.WriteLine(str);
You are finding it in the array, but you should find the word in the string.
Use following if you want to check:
drillDownUniqueNameArray.Any(x=>x.Contains("Sales"));
Use following if want to get the strings which contains "Sales"
drillDownUniqueNameArray.Where(x=>x.Contains("Sales"));
When you do it like this:
drillDownUniqueNameArray.Contains("S")
it's not gonna check the values, you must do it like this:
drillDownUniqueNameArray[0].Contains("S") or drillDownUniqueNameArray.First().Contains("S")
like this way it checks the values inside the array not the arrays itself
I have string file with content like this
string a = "12,1,______,_,__;1,23,122;"
I want to grab only this integer value 23. Problem is that this conntent is dynamic and I it's lenght can be changed, so this string a can easily be in the next iteration like
string a = "12,1,______,_,__;11,2,1;"
In this case I would grab integer 2.
If the structure is always the same, then:
Split the string, and grab the element before last.
var array1 = a.Split(';');
// check the array length if it's needed to avoid
// IndexOutOfRangeException exception
var array2 = array1[1].Split(',');
var yourNumber = array2[array2.Length - 2]
String.Split
Ignoring error checking for a minute, this would work:
string a = "12,1,______,_,__;11,2,1;"
int i = Int32.Parse(String.Split(',')[5])
If this is the route you will go, you should take extra care to verify your input. Check the length of the array reutrned from Split, and verify that the 5th value can indeed be parsed to an int.
Try this regex:
(?<=;\d*,)\d*(?=,\d*;)
Sample usage:
class Program
{
static void Main(string[] args)
{
string a = "12,1,______,_,__;1,23,122;";
var regex = new Regex(#"(?<=;\d*,)\d*(?=,\d*;)");
Console.WriteLine(regex.Match(a).Value);
a = "12,1,______,_,__;11,2,1;";
Console.WriteLine(regex.Match(a).Value);
}
}
Try this:
var number = a.split(",")[5];
another option is to split the text into array (if they have same pattern):
var output = a.Split(",;".ToCharArray());
var value = output[theIndex]; // get the index you want