string manipulation in c#
-
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.
-
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.
Your question is very interesting Proposed to use " " handle a little better, you say?
-
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.
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.
-
Your question is very interesting Proposed to use " " handle a little better, you say?
-
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
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.
-
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.
-
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.
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
-
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?
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
GOAnd 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.
-
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
-
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.
-
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
-
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
:thumbsup:
Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.
-
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.
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.
-
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.
-
May be They Dont like U..! ;P Check now..!
LatestArticle :Log4Net Why Do Some People Forget To Mark as Answer .If It Helps.
:laugh: .
Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.
-
:thumbsup:
Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.
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
-
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
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;
}
}