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. Typical String Manipulation [modified]

Typical String Manipulation [modified]

Scheduled Pinned Locked Moved ASP.NET
questioncsharptutorial
10 Posts 4 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.
  • S Offline
    S Offline
    Satish Developer
    wrote on last edited by
    #1

    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

    S J N 3 Replies Last reply
    0
    • S Satish Developer

      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

      S Offline
      S Offline
      Sneha Bisht
      wrote on last edited by
      #2

      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......

      S N 2 Replies Last reply
      0
      • S Satish Developer

        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

        J Offline
        J Offline
        JHizzle
        wrote on last edited by
        #3

        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.

        S 1 Reply Last reply
        0
        • J JHizzle

          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.

          S Offline
          S Offline
          Satish Developer
          wrote on last edited by
          #4

          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

          J 1 Reply Last reply
          0
          • S Sneha Bisht

            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......

            S Offline
            S Offline
            Satish Developer
            wrote on last edited by
            #5

            Thanks for ur funcion. But its not meeting my criteria..it giving some wrong results.

            G. Satish

            1 Reply Last reply
            0
            • S Sneha Bisht

              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......

              N Offline
              N Offline
              Not Active
              wrote on last edited by
              #6

              Satish - Developer wrote:

              How can i get this in c#?

              This is not C#


              I know the language. I've read a book. - _Madmatt

              1 Reply Last reply
              0
              • S Satish Developer

                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

                N Offline
                N Offline
                Not Active
                wrote on last edited by
                #7

                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

                S 1 Reply Last reply
                0
                • N Not Active

                  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

                  S Offline
                  S Offline
                  Satish Developer
                  wrote on last edited by
                  #8

                  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

                  N 1 Reply Last reply
                  0
                  • S Satish Developer

                    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

                    N Offline
                    N Offline
                    Not Active
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    • S Satish Developer

                      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

                      J Offline
                      J Offline
                      JHizzle
                      wrote on last edited by
                      #10

                      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.

                      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