none
How to cut string in c #? RRS feed

  • Question

  • Dear all.

    I have the string as below:

    string a=" ======== WEIGHT ========\r\n WELCOME TO CAS          \r\n DATE. 2000/01/01\r\n TIME. 00:00:80\r\n\r\n  Weight :       0.22kg\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n"

    How to get the character 0.22?

    Thanks all.

    Wednesday, December 11, 2019 8:44 AM

All replies


  • I have the string as below:

    string a=" ======== WEIGHT ========\r\n WELCOME TO CAS          \r\n DATE. 2000/01/01\r\n TIME. 00:00:80\r\n\r\n  Weight :       0.22kg\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n"

    How to get the character 0.22?


    I suppose there are many ways to parse that line, probably including using
    regular expressions. Since I don't have much hair left I'll leave that approach
    for someone else. Here's one of many alternative methods:

    class Program
    {
        static void Main(string[] args)
        {
            string a = " ======== WEIGHT ========\r\n WELCOME TO CAS          \r\n DATE. 2000/01/01\r\n TIME. 00:00:80\r\n\r\n  Weight :       0.22kg\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n";
            var sal = a.Split().ToList();
            sal.RemoveAll(EmptyLine);        
            for(int n = 0; n < sal.Count; ++n)
            {
                if (sal[n] == "Weight")                                       
            }      Console.WriteLine(sal[n + 2].TrimEnd("kg".ToCharArray()));
        }                                                                                     
        private static bool EmptyLine(String s)
        {            
            return s == "";
        }
    }
    

    - Wayne

    Wednesday, December 11, 2019 9:55 AM
  • Thanks you very much <3
    Wednesday, December 11, 2019 1:14 PM
  • Hi Studyandshare,
    Has your problem been solved? If it is resolved, we suggest that you mark it as the answer. So it can help other people who have the same problem find a solution quickly.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 25, 2019 6:45 AM