Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. string manipulation in c#

string manipulation in c#

Scheduled Pinned Locked Moved ASP.NET
csharphelptutorial
17 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U uglyeyes

    Hi! I have below text. "A quick sly fox jumped over the lazy brown dog" "This is my rifle, this is my gun. this is for fight and this is for fun." I need to programmatically get first 7 words without breaking any words. for eg. from first text I need "A quick sly fox jumped over" and from second "This is my rifle, this is" could someone please help me how to get this.

    L Offline
    L Offline
    lrsoft2009
    wrote on last edited by
    #2

    Your question is very interesting Proposed to use " " handle a little better, you say?

    U 1 Reply Last reply
    0
    • U uglyeyes

      Hi! I have below text. "A quick sly fox jumped over the lazy brown dog" "This is my rifle, this is my gun. this is for fight and this is for fun." I need to programmatically get first 7 words without breaking any words. for eg. from first text I need "A quick sly fox jumped over" and from second "This is my rifle, this is" could someone please help me how to get this.

      A Offline
      A Offline
      Abhijit Jana
      wrote on last edited by
      #3

      How does it related with ASP.NET ? This is simple - String manipulation. You can use String.Split to split the string and then get the 7 words :) .

      Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

      1 Reply Last reply
      0
      • L lrsoft2009

        Your question is very interesting Proposed to use " " handle a little better, you say?

        U Offline
        U Offline
        uglyeyes
        wrote on last edited by
        #4

        I have like 1000 rows returning from database is string.split and looping to get the 7 words appropriate way to go? could you please send me an efficient way to do this so it could load in web page faster

        A 1 Reply Last reply
        0
        • U uglyeyes

          I have like 1000 rows returning from database is string.split and looping to get the 7 words appropriate way to go? could you please send me an efficient way to do this so it could load in web page faster

          A Offline
          A Offline
          Abhijit Jana
          wrote on last edited by
          #5

          uglyeyes wrote:

          I have like 1000 rows returning from database is string.split and looping to get the 7 words appropriate way to go?

          Then why you are not doing this thing in DB End ? Make the stored procedure which will return those first 7 words from DB rather than whole sentence.

          Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

          U A 2 Replies Last reply
          0
          • A Abhijit Jana

            uglyeyes wrote:

            I have like 1000 rows returning from database is string.split and looping to get the 7 words appropriate way to go?

            Then why you are not doing this thing in DB End ? Make the stored procedure which will return those first 7 words from DB rather than whole sentence.

            Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

            U Offline
            U Offline
            uglyeyes
            wrote on last edited by
            #6

            I am not very good in DB could you pleae help? and do I really need a stored proc for this. can it be not done in a single line?

            A 1 Reply Last reply
            0
            • U uglyeyes

              Hi! I have below text. "A quick sly fox jumped over the lazy brown dog" "This is my rifle, this is my gun. this is for fight and this is for fun." I need to programmatically get first 7 words without breaking any words. for eg. from first text I need "A quick sly fox jumped over" and from second "This is my rifle, this is" could someone please help me how to get this.

              T Offline
              T Offline
              Tripathi Swati
              wrote on last edited by
              #7

              split using space n make one for loop which will go upto lenght where take one variable which is going up to 7 and again reset to 1. dis manner u can get. and yes store this result in list ...

              Reasons are not Important but Results are Important. Swati Tripathi

              U 1 Reply Last reply
              0
              • U uglyeyes

                I am not very good in DB could you pleae help? and do I really need a stored proc for this. can it be not done in a single line?

                A Offline
                A Offline
                Abhijit Jana
                wrote on last edited by
                #8

                uglyeyes wrote:

                could you pleae help?

                PL SQL Function :

                CREATE FUNCTION SplitString
                (
                -- Add the parameters for the function here
                @myString varchar(500),
                @deliminator varchar(10)
                )
                RETURNS
                @ReturnTable TABLE
                (
                -- Add the column definitions for the TABLE variable here
                [id] [int] IDENTITY(1,1) NOT NULL,
                [part] [varchar](50) NULL
                )
                AS
                BEGIN
                Declare @iSpaces int
                Declare @part varchar(50)

                    --initialize spaces
                    Select @iSpaces = charindex(@deliminator,@myString,0)
                    While @iSpaces > 0
                
                    Begin
                        Select @part = substring(@myString,0,charindex(@deliminator,@myString,0))
                
                        Insert Into @ReturnTable(part)
                        Select @part
                
                Select @myString = substring(@mystring,charindex(@deliminator,@myString,0)+ len(@deliminator),len(@myString) - charindex(' ',@myString,0))
                
                
                        Select @iSpaces = charindex(@deliminator,@myString,0)
                    end
                
                    If len(@myString) > 0
                        Insert Into @ReturnTable
                        Select @myString
                
                RETURN 
                

                END
                GO

                And Try with Example

                Select * From SplitString('Hello John Smith',' ')

                Original Source[^] :)

                Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

                1 Reply Last reply
                0
                • T Tripathi Swati

                  split using space n make one for loop which will go upto lenght where take one variable which is going up to 7 and again reset to 1. dis manner u can get. and yes store this result in list ...

                  Reasons are not Important but Results are Important. Swati Tripathi

                  U Offline
                  U Offline
                  uglyeyes
                  wrote on last edited by
                  #9

                  hello either in sql or c# a code hint will be really helpful. I prefer in sql thou.

                  A U 2 Replies Last reply
                  0
                  • U uglyeyes

                    hello either in sql or c# a code hint will be really helpful. I prefer in sql thou.

                    A Offline
                    A Offline
                    Abhijit Jana
                    wrote on last edited by
                    #10

                    uglyeyes wrote:

                    hello either in sql or c# a code hint will be really helpful. I prefer in sql thou.

                    Try with sample that I have provided to you !

                    Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

                    1 Reply Last reply
                    0
                    • U uglyeyes

                      hello either in sql or c# a code hint will be really helpful. I prefer in sql thou.

                      U Offline
                      U Offline
                      uglyeyes
                      wrote on last edited by
                      #11

                      this one worked. DECLARE @mystring VARCHAR(200) DECLARE @count INT DECLARE @max INT DECLARE @space INT SET @mystring = 'A quick sly fox jumped over the lazy brown dog' SET @count = 0 SET @max = 7 SET @space = 1 SET NOCOUNT ON WHILE @count < @max BEGIN SELECT @space = CHARINDEX (' ', @mystring , @space + 1) -- SELECT 'position: ', @space SET @count = @count + 1 END SELECT LEFT(@mystring,@space) SET NOCOUNT OFF

                      A 1 Reply Last reply
                      0
                      • U uglyeyes

                        this one worked. DECLARE @mystring VARCHAR(200) DECLARE @count INT DECLARE @max INT DECLARE @space INT SET @mystring = 'A quick sly fox jumped over the lazy brown dog' SET @count = 0 SET @max = 7 SET @space = 1 SET NOCOUNT ON WHILE @count < @max BEGIN SELECT @space = CHARINDEX (' ', @mystring , @space + 1) -- SELECT 'position: ', @space SET @count = @count + 1 END SELECT LEFT(@mystring,@space) SET NOCOUNT OFF

                        A Offline
                        A Offline
                        Abhijit Jana
                        wrote on last edited by
                        #12

                        :thumbsup:

                        Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

                        U 1 Reply Last reply
                        0
                        • A Abhijit Jana

                          uglyeyes wrote:

                          I have like 1000 rows returning from database is string.split and looping to get the 7 words appropriate way to go?

                          Then why you are not doing this thing in DB End ? Make the stored procedure which will return those first 7 words from DB rather than whole sentence.

                          Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

                          A Offline
                          A Offline
                          Abhijit Jana
                          wrote on last edited by
                          #13

                          Why somebody voted it as 1 ? Any Proper reason ?

                          Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

                          S 1 Reply Last reply
                          0
                          • A Abhijit Jana

                            Why somebody voted it as 1 ? Any Proper reason ?

                            Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

                            S Offline
                            S Offline
                            sashidhar
                            wrote on last edited by
                            #14

                            May be They Dont like U..! ;P Check now..!

                            LatestArticle :Log4Net Why Do Some People Forget To Mark as Answer .If It Helps.

                            A 1 Reply Last reply
                            0
                            • S sashidhar

                              May be They Dont like U..! ;P Check now..!

                              LatestArticle :Log4Net Why Do Some People Forget To Mark as Answer .If It Helps.

                              A Offline
                              A Offline
                              Abhijit Jana
                              wrote on last edited by
                              #15

                              :laugh: .

                              Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

                              1 Reply Last reply
                              0
                              • A Abhijit Jana

                                :thumbsup:

                                Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

                                U Offline
                                U Offline
                                uglyeyes
                                wrote on last edited by
                                #16

                                Ok, I think via database it works but I need to use this logic via c# code. could someone please show me how to write a function that will give me same output. thanks in advance below code runs two times and prints it twice. how can i make it more efficient so it loads faster and dispalys only once?

                                public string getAdBrief(string text)
                                {
                                string s = "";
                                string[] words = text.Split(' ');
                                for (int i = 0; i < 7; i++)
                                {
                                s +=words[i] + ' ';
                                }
                                return s;
                                }

                                modified on Wednesday, November 18, 2009 9:30 PM

                                U 1 Reply Last reply
                                0
                                • U uglyeyes

                                  Ok, I think via database it works but I need to use this logic via c# code. could someone please show me how to write a function that will give me same output. thanks in advance below code runs two times and prints it twice. how can i make it more efficient so it loads faster and dispalys only once?

                                  public string getAdBrief(string text)
                                  {
                                  string s = "";
                                  string[] words = text.Split(' ');
                                  for (int i = 0; i < 7; i++)
                                  {
                                  s +=words[i] + ' ';
                                  }
                                  return s;
                                  }

                                  modified on Wednesday, November 18, 2009 9:30 PM

                                  U Offline
                                  U Offline
                                  uglyeyes
                                  wrote on last edited by
                                  #17

                                  never mind this helped

                                  public static string GetFirstFewWords(string input, int numberWords)
                                  {
                                  if (input.Split(new char[] { ' ' },
                                  StringSplitOptions.RemoveEmptyEntries).Length > numberWords)
                                  {
                                  // Number of words we still want to display.
                                  int words = numberWords;
                                  // Loop through entire summary.
                                  for (int i = 0; i < input.Length; i++)
                                  {
                                  // Increment words on a space.
                                  if (input[i] == ' ')
                                  {
                                  words--;
                                  }
                                  // If we have no more words to display, return the substring.
                                  if (words == 0)
                                  {
                                  return input.Substring(0, i);
                                  }
                                  }
                                  return string.Empty;
                                  }
                                  else
                                  {
                                  return input;
                                  }
                                  }

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups