locked
removing three consecutive dots in a string "..." RRS feed

  • Question

  • Hi,

    I have a m-file and at some places there are three consecutive dots. e.g

    HtTest_time =  [0, 2,  2+r,  4,  4+r,  6,  6+r,  8,  8+r, 10,...     10+r, 12, 12+r, 14, 14+r, 16, 16+r, 18, 18+r, 20,...     20+r, 22 ];

    I want to remove all instances of these three consecutive dots. There can be dots somewhere else those should remain unchanged.

    I am reading file line by line using streamreader.


    using (StreamReader sr = new StreamReader(@"C:\inputFile.m"))
                {

                    string s;
                    while ((s = sr.ReadLine()) != null)
                    {

                       s = (removing all instances of three consecutive dots...   if there are any);
                        string[] sArr = s.Split("=".ToCharArray());

                        string s1 = sArr[0];
                        string s2 = sArr[1]


    later i want to parse it
    right now i am using a long long  if (s.Contains"") thing.

    I also need help with some REGEX  like if  s2 starts with "[" then anything, then "]" then "+" or "-" or "*" or "/" then anything  and ";" at the end


    Thanks a lot for help.

    BR
    Thursday, November 26, 2009 3:18 PM

Answers

  • For removing the string occurrences, you can utilize the string.Replace method.

    so you will be doing something like

    s = s.Replace("...", string.Empty);

    For Regular Expression, you can use the RegEx class.

    string pattern = "\[\s*\](+|-|*|/)\s*;


    Thanks,
    A.m.a.L
    Dot Net Goodies
    Don't hate the hacker, hate the code
    • Edited by A.m.a.L Hashim Thursday, November 26, 2009 3:31 PM added regex
    • Marked as answer by arsalancheema Thursday, November 26, 2009 3:50 PM
    Thursday, November 26, 2009 3:25 PM
  • Do you mean

    s.Replace("...","")

    ??
    Florian Kowalsky
    • Marked as answer by arsalancheema Thursday, November 26, 2009 3:51 PM
    Thursday, November 26, 2009 3:26 PM

All replies

  • For removing the string occurrences, you can utilize the string.Replace method.

    so you will be doing something like

    s = s.Replace("...", string.Empty);

    For Regular Expression, you can use the RegEx class.

    string pattern = "\[\s*\](+|-|*|/)\s*;


    Thanks,
    A.m.a.L
    Dot Net Goodies
    Don't hate the hacker, hate the code
    • Edited by A.m.a.L Hashim Thursday, November 26, 2009 3:31 PM added regex
    • Marked as answer by arsalancheema Thursday, November 26, 2009 3:50 PM
    Thursday, November 26, 2009 3:25 PM
  • Do you mean

    s.Replace("...","")

    ??
    Florian Kowalsky
    • Marked as answer by arsalancheema Thursday, November 26, 2009 3:51 PM
    Thursday, November 26, 2009 3:26 PM
  • Thank you very much.

    I more question, might be very stupid one,  bur regex always scare me....



    for this regular expression

    string pattern = "\[\s*\](+|-|*|/)\s*;

    how can i use it in    if statement?
    like

    if (s match "\[\s*\](+|-|*|/)\s* )
    {

    }

    what is the proper way??



    thanks...
    Thursday, November 26, 2009 3:41 PM
  • No need to get scared :-)

    Please go through the RegEx link I have posted. That site contains detailed explanation.


    Thanks,
    A.m.a.L
    Dot Net Goodies
    Don't hate the hacker, hate the code
    Thursday, November 26, 2009 3:45 PM