How can I parse only the float number from the string?

前端 未结 3 1236
悲&欢浪女
悲&欢浪女 2021-01-21 19:27
foreach (object item in listBox1.SelectedItems)
{
    string curItem = item.ToString();
    var parts = curItem.Split(\"{}XY=, \".ToCharArray(), StringSplitOptions.Remov         


        
3条回答
  •  半阙折子戏
    2021-01-21 19:59

    You can simply extract the float from the string by using Regular Expressions:

    string xCoord = Regex.Match(curItem, @"[-+]?[0-9]*\.?[0-9]+").Groups[1].Value;
    

    After that, you can parse it to a float.

    More info about regular expressions can be found here, or you could take a look at the Regex class page from MSDN.

提交回复
热议问题