I need to figure out how to remove the end of a string. The only trouble is, the string itself is not set. All I want to keep is the first 3-4 characters in the string.
string Location = "110 - Main Road";
string Location = "123 - Highway";
string Location = "234 - My House";It could also be;
string Location = "1120 - Main Road";I know if I can cut it down to the first 4 characters, I can just use .Trim() to remove the white spaces if it is only 3 characters, but I don't know how to cut it down to the first 4 characters?
7 Answers
You can use Split and get your number like this:
string Location = "1120 - Main Road";
int number = int.Parse(Location.Split()[0]);This should work if there is no white-space before the number.If there is then use StringSplitOptions.RemoveEmptyEntries:
int number = int.Parse(Location.Split(new []{ ' ' }, StringSplitOptions.RemoveEmptyEntries)[0]); 2 split on spaces, then grab whatever is first, ignore the rest.
string GrabNumber(string input)
{ return input.Split(' ')[0];
}assuming you want it as an integer you can take it a step further:
int GrabNumber(string input)
{ return int.Parse(input.Split(' ')[0]);
} 0 You can use String.Split() function to split your string based on delimeter - and then you can convert the first part of the string into integer if you want it in a Integer variable.
Solution 1: if you want to get first part of string as as string.
string Location = "11056 - Main Road";
Location = Location.Split('-')[0].Trim();Solution 2: if you want to get the first part of the string as integer value.
string Location = "11056 - Main Road";
int num;
int.TryParse(Location.Split('-')[0],out num); Just use a Substring call with a String.IndexOf, for example
using System;
using System.Collections.Generic;
public class Test
{ public static void Main() { List<string> strings = new List<string>(); strings.Add("110 - Main Road"); strings.Add("1104 - Main Road"); strings.Add("11088 - Main Road"); foreach(string s in strings){ Console.WriteLine(s.Substring(0,s.IndexOf("-",0)-1)); } }
}That way even if the street number is 4,5,6,7 characters long this will still work
If you just want the first 4 characters you would do this:
Location = Location.Substring(0, 4);The first argument is the start position and the second argument is the length.
1use the substring function of string(startIndex,numberOfCharToKeep) like this:
string Location = "110 - Main Road";
string NewLocation = Location.SubString(0,4); this keeps your first 4 chars
Depends on how reliable your input is. If you will always have a space after the numbers you can find that location using IndexOf. However, whenever I work with strings I prefer regular expressions. Here is an example of both approaches:
using System;
using System.Text.RegularExpressions;
class Program
{ static void Main(string[] args) { string[] locations = { "110 - Main Road", "123 - Highway", "234 - My House", "1120 - Main Road" }; Regex r = new Regex(@"^\d+"); foreach (string location in locations) { Console.WriteLine(location.Substring(0, location.IndexOf(' '))); Console.WriteLine(r.Match(location).Captures[0].Value); } }
}