Ok So i have something like this:
String str = "Have.Fun.Something";
String str2 = "asd.sad.saw.sa.Something";
and i wanna remove all things from last char until first "." so output will be always
Something
Ok So i have something like this:
String str = "Have.Fun.Something";
String str2 = "asd.sad.saw.sa.Something";
and i wanna remove all things from last char until first "." so output will be always
Something
There are a whole bunch of useful methods on an instance of a string. The best place for you to look is on MSDN.
In your particular case you can use substring and lastindexof. If the case were more complicated I'd probably suggest RegEx although this is much more involved.
String str2 = str.SubString(str.LastIndexOf('.') + 1);
You Can use Split function Below like this :
String str = "Have.Fun.Something";
String str2 = "asd.sad.saw.sa.Something";
string[] Words= str.Split('.');
MessageBox.Show(Words[Words.Length-1].tostring());
Get Below like this
Something
Same as String str2 = "asd.sad.saw.sa.Something"; Thanks..!
You may use Regular Expressions (regex):
(?:.*)\.(?<myGroup>.*)
Applying the above regex, you'll have a group named 'myGroup' which has the value after the last dot
Personally, i like this options because it gives you further scalability if the need arises
If you need further assistance with regex, please say so
Bug free approach
public static string Substring(string input)
{
if (!String.IsNullOrEmpty(input))
{
int lastIndex = input.LastIndexOf('.');
if (lastIndex != -1)
return input.Substring(lastIndex + 1);
}
return input;
}