Getting portion of string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RP

    #1

    Getting portion of string

    I have a string in the following format

    Computer [comp]

    I want to remove the text that starts with [. Though I used substring
    which keeps checking whether the character is [ and stops reading more
    characters. I want to know is there any built-in function for this
    that is both fast and easy to use.
  • Luuk

    #2
    Re: Getting portion of string


    "RP" <rpk.general@gm ail.comschreef in bericht
    news:9e4e27ae-6af9-4589-ae5b-025e452bb902@i2 9g2000prf.googl egroups.com...
    >I have a string in the following format
    >
    Computer [comp]
    >
    I want to remove the text that starts with [. Though I used substring
    which keeps checking whether the character is [ and stops reading more
    characters. I want to know is there any built-in function for this
    that is both fast and easy to use.
    google:


    1st link:

    (because you're obviously tooo lazy to start Google in the 1st place...)



    Comment

    • Yasir Zaheer

      #3
      Re: Getting portion of string

      if you only want to get the string before '['. try ...

      string str = "Computer[comp]";
      str = str.Substring(0 , str.IndexOf("["));

      if there are multiple "[]" in your string try this.

      string str = "Comp[cooom] makes [power] one cry";
      string[] array = str.Split(']');
      string finishedString = string.Empty;
      foreach (string member in array)
      {
      if (member.Contain s("["))
      finishedString += member.Substrin g(0, member.IndexOf( "["));
      else
      finishedString += member;
      }



      Regards,

      On Dec 25, 6:49 pm, "Luuk" <l...@invalid.l anwrote:
      "RP" <rpk.gene...@gm ail.comschreef in berichtnews:9e4 e27ae-6af9-4589-ae5b-025e452bb902@i2 9g2000prf.googl egroups.com...
      >
      I have a string in the following format
      >
      Computer [comp]
      >
      I want to remove the text that starts with [. Though I used substring
      which keeps checking whether the character is [ and stops reading more
      characters. I want to know is there any built-in function for this
      that is both fast and easy to use.
      >
      google:http://www.google.com/search?source=...arp+substring&...
      >
      1st link:http://en.csharp-online.net/Manipula...arp%E2%80%94Fi...
      (because you're obviously tooo lazy to start Google in the 1st place...)

      Comment

      • Greg

        #4
        Re: Getting portion of string

        On Dec 25, 5:41 am, RP <rpk.gene...@gm ail.comwrote:
        I have a string in the following format
        >
        Computer [comp]
        >
        I want to remove the text that starts with [. Though I used substring
        which keeps checking whether the character is [ and stops reading more
        characters. I want to know is there any built-in function for this
        that is both fast and easy to use.
        I don't think there is any built in String functionality.
        You might try this:

        string str = "Comp[cooom] makes [power] one cry";
        while (str.IndexOf('[') >= 0 || str.IndexOf(']') >= 0)
        {
        int start = str.IndexOf("[");
        int end = str.IndexOf("]");
        str = str.Remove(star t, (end - start + 1));
        }
        Console.WriteLi ne(str);

        Depending on the size of the string, you might consider using the
        StringBuilder class.

        Regards,
        Greg


        Comment

        Working...