Typical String Manipulation [modified]
-
Hi, the requirement looks easily but i am not knowing how to achieve this. i have two strings as below
string _mainstr = "INDI,GERM,ENGL,SCOT,BLAC,BANG,CHIN,JAPN";
string _substr = "CHIN,ENGL,INDI";Now i want to remove all other country names from _mainstr which are not in _substr. But the order shouldn't be changed. I am looking an ouput from _mainstr as follows _mainstr = "INDI,ENGL,CHIN"; Here the order is important. How can i get this in c#? Please give an idea
G. Satish
modified on Thursday, March 18, 2010 5:50 AM
-
Hi, the requirement looks easily but i am not knowing how to achieve this. i have two strings as below
string _mainstr = "INDI,GERM,ENGL,SCOT,BLAC,BANG,CHIN,JAPN";
string _substr = "CHIN,ENGL,INDI";Now i want to remove all other country names from _mainstr which are not in _substr. But the order shouldn't be changed. I am looking an ouput from _mainstr as follows _mainstr = "INDI,ENGL,CHIN"; Here the order is important. How can i get this in c#? Please give an idea
G. Satish
modified on Thursday, March 18, 2010 5:50 AM
First create function in database(for split these val;ues) CREATE FUNCTION [dbo].[Split](@String varchar(8000), @Delimiter char(1)) returns @temptable TABLE (i int identity(1,1), items varchar(8000)) as begin declare @idx int declare @slice varchar(8000) select @idx = 1 if len(@String)<1 or @String is null return while @idx!= 0 begin set @idx = charindex(@Delimiter,@String) if @idx!=0 set @slice = left(@String,@idx - 1) else set @slice = @String if(len(@slice)>0) insert into @temptable(Items) values(@slice) set @String = right(@String,len(@String) - @idx) if len(@String) = 0 break end return after that create sp(that should be like that ) create storedprocedure [dbo].[sp_notINMain] @_mainstr navrchar(1000), @_substr nvarchra(1000) begin as select a.items , b.items from dbo.split(@_mainstr,',') a inner join dbo.split(@_substr,',') b on a.i = b.i where a.items not in(b.items) order by a.i asc end Try This may HELP......
-
Hi, the requirement looks easily but i am not knowing how to achieve this. i have two strings as below
string _mainstr = "INDI,GERM,ENGL,SCOT,BLAC,BANG,CHIN,JAPN";
string _substr = "CHIN,ENGL,INDI";Now i want to remove all other country names from _mainstr which are not in _substr. But the order shouldn't be changed. I am looking an ouput from _mainstr as follows _mainstr = "INDI,ENGL,CHIN"; Here the order is important. How can i get this in c#? Please give an idea
G. Satish
modified on Thursday, March 18, 2010 5:50 AM
use .Split off substr to create an array of searched for strings. Iterate through this against _mainstr. With a stringbuilder, if you've got a match, append this to a holding object. At the end of it your stringbuilder will have the matched strings in the right order.
-
use .Split off substr to create an array of searched for strings. Iterate through this against _mainstr. With a stringbuilder, if you've got a match, append this to a holding object. At the end of it your stringbuilder will have the matched strings in the right order.
Pls Can you give code for this? I tried by i am not getting correct order. i am getting result what i have in substr.
string _mainstr = "INDI,GERM,ENGL,SCOT,BLAC,BANG,CHIN,JAPN";
string _substr = "CHIN,ENGL,INDI";
ArrayList ary = new ArrayList();string\[\] sub = \_substr.Split(','); for (int i = 0; i < sub.Length; i++) { if (\_mainstr.Contains(sub\[i\].ToString())) { ary.Add(sub\[i\]); } }
G. Satish
-
First create function in database(for split these val;ues) CREATE FUNCTION [dbo].[Split](@String varchar(8000), @Delimiter char(1)) returns @temptable TABLE (i int identity(1,1), items varchar(8000)) as begin declare @idx int declare @slice varchar(8000) select @idx = 1 if len(@String)<1 or @String is null return while @idx!= 0 begin set @idx = charindex(@Delimiter,@String) if @idx!=0 set @slice = left(@String,@idx - 1) else set @slice = @String if(len(@slice)>0) insert into @temptable(Items) values(@slice) set @String = right(@String,len(@String) - @idx) if len(@String) = 0 break end return after that create sp(that should be like that ) create storedprocedure [dbo].[sp_notINMain] @_mainstr navrchar(1000), @_substr nvarchra(1000) begin as select a.items , b.items from dbo.split(@_mainstr,',') a inner join dbo.split(@_substr,',') b on a.i = b.i where a.items not in(b.items) order by a.i asc end Try This may HELP......
Thanks for ur funcion. But its not meeting my criteria..it giving some wrong results.
G. Satish
-
First create function in database(for split these val;ues) CREATE FUNCTION [dbo].[Split](@String varchar(8000), @Delimiter char(1)) returns @temptable TABLE (i int identity(1,1), items varchar(8000)) as begin declare @idx int declare @slice varchar(8000) select @idx = 1 if len(@String)<1 or @String is null return while @idx!= 0 begin set @idx = charindex(@Delimiter,@String) if @idx!=0 set @slice = left(@String,@idx - 1) else set @slice = @String if(len(@slice)>0) insert into @temptable(Items) values(@slice) set @String = right(@String,len(@String) - @idx) if len(@String) = 0 break end return after that create sp(that should be like that ) create storedprocedure [dbo].[sp_notINMain] @_mainstr navrchar(1000), @_substr nvarchra(1000) begin as select a.items , b.items from dbo.split(@_mainstr,',') a inner join dbo.split(@_substr,',') b on a.i = b.i where a.items not in(b.items) order by a.i asc end Try This may HELP......
Satish - Developer wrote:
How can i get this in c#?
This is not C#
I know the language. I've read a book. - _Madmatt
-
Hi, the requirement looks easily but i am not knowing how to achieve this. i have two strings as below
string _mainstr = "INDI,GERM,ENGL,SCOT,BLAC,BANG,CHIN,JAPN";
string _substr = "CHIN,ENGL,INDI";Now i want to remove all other country names from _mainstr which are not in _substr. But the order shouldn't be changed. I am looking an ouput from _mainstr as follows _mainstr = "INDI,ENGL,CHIN"; Here the order is important. How can i get this in c#? Please give an idea
G. Satish
modified on Thursday, March 18, 2010 5:50 AM
string _mainstr = "INDI,GERM,ENGL,SCOT,BLAC,BANG,CHIN,JAPN";
string _substr = "CHIN,ENGL,INDI";List main = new List(_mainstr.Split(','));
List sub = new List(_substr.Split(','));main = main.FindAll(delegate(string s) { return sub.Contains(s); });
I know the language. I've read a book. - _Madmatt
-
string _mainstr = "INDI,GERM,ENGL,SCOT,BLAC,BANG,CHIN,JAPN";
string _substr = "CHIN,ENGL,INDI";List main = new List(_mainstr.Split(','));
List sub = new List(_substr.Split(','));main = main.FindAll(delegate(string s) { return sub.Contains(s); });
I know the language. I've read a book. - _Madmatt
Thank you. But the code u have given is in DotNet 3.0 I am working in DotNet 2.0. Please give code which works in 2.0. Thank you
G. Satish
-
Thank you. But the code u have given is in DotNet 3.0 I am working in DotNet 2.0. Please give code which works in 2.0. Thank you
G. Satish
You should have specified the version of the framework you are working with initially. You're on your own.
I know the language. I've read a book. - _Madmatt
-
Pls Can you give code for this? I tried by i am not getting correct order. i am getting result what i have in substr.
string _mainstr = "INDI,GERM,ENGL,SCOT,BLAC,BANG,CHIN,JAPN";
string _substr = "CHIN,ENGL,INDI";
ArrayList ary = new ArrayList();string\[\] sub = \_substr.Split(','); for (int i = 0; i < sub.Length; i++) { if (\_mainstr.Contains(sub\[i\].ToString())) { ary.Add(sub\[i\]); } }
G. Satish
Right having thought about this a bit more, I realize my pseudo code was a bit off. You're close though, you just need to split _mainstr into an array too then iterate through that and iterate through your _substr array too so: Loop through _mainstr array Within each loop, loop through _substr array and see if it's there, if so, add to holding object. Holding object at the end = matched items in the original order.