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. General Programming
  3. C#
  4. concateing strings in c#

concateing strings in c#

Scheduled Pinned Locked Moved C#
csharphelp
16 Posts 3 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.
  • L Offline
    L Offline
    lawrenceinba
    wrote on last edited by
    #1

    hi friends i want to implement a logic .. string input will be given...it should merge only single characters..pls help eg.. behind r m d street o/p expected is behind rmd street any body help me out with apt logic......

    the quieter u become more u hear

    M G 2 Replies Last reply
    0
    • L lawrenceinba

      hi friends i want to implement a logic .. string input will be given...it should merge only single characters..pls help eg.. behind r m d street o/p expected is behind rmd street any body help me out with apt logic......

      the quieter u become more u hear

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      how bout a function along the lines of the following

      string MergeFunction(string value)
      {
      result = value;

      for(int i = 0; i < value.length; i++)
      {

      if(value[i-1] == ' ' && value[i+1] == ' ')//single char
      {
      result = result.remove(i+1, 1);
      }

      }

      return result;
      }

      dont forget you also need to include some check for handling if i == 0; or i == value.length -1; because it will throw a index out of bounds error as it is. EDIT: oops you will need some sort of count for the number of spaces currently remove so you can subtract it from the value when using result.remove result.remove(i+1-count, 1);

      My opinion is... If someone has already posted an answer, dont post the SAME answer

      L 1 Reply Last reply
      0
      • M musefan

        how bout a function along the lines of the following

        string MergeFunction(string value)
        {
        result = value;

        for(int i = 0; i < value.length; i++)
        {

        if(value[i-1] == ' ' && value[i+1] == ' ')//single char
        {
        result = result.remove(i+1, 1);
        }

        }

        return result;
        }

        dont forget you also need to include some check for handling if i == 0; or i == value.length -1; because it will throw a index out of bounds error as it is. EDIT: oops you will need some sort of count for the number of spaces currently remove so you can subtract it from the value when using result.remove result.remove(i+1-count, 1);

        My opinion is... If someone has already posted an answer, dont post the SAME answer

        L Offline
        L Offline
        lawrenceinba
        wrote on last edited by
        #3

        can u give me the entire code with my given input.... pls... it shows out of bound error even i do as u said... pls help me out

        the quieter u become more u hear

        M 1 Reply Last reply
        0
        • L lawrenceinba

          can u give me the entire code with my given input.... pls... it shows out of bound error even i do as u said... pls help me out

          the quieter u become more u hear

          M Offline
          M Offline
          musefan
          wrote on last edited by
          #4

          Ok as its not too much code, though im not testing so let me know any errors

          ///<summary>
          ///Merges any characters on there own
          ///<summary>
          ///<param name="s">The string to parse</param>
          string MergeFunction(string s)
          {
          string result = s;
          int count 0;//used to count num of removed chars, this is because length of result may get shorter
          //than the length of s due to removal of spaces
          for(int i = 0; i < s.Length-1; i++)//no need to test last char so use s.length -1
          {
          if(i == 0)//if first char
          {
          if(s[i+1] == ' ')//check if next char is a space
          {
          result = result.Remove(i+1-count, 1);
          count++;
          }
          }
          else//any char other than the first and last(due to for loop limit)
          {
          if(s[i-1] == ' ' && s[i+1] == ' ')//check if both sides of char is a space
          {
          result = result.Remove(i+1-count, 1);
          count++;
          }

          }

          }

          return result;
          }

          My opinion is... If someone has already posted an answer, dont post the SAME answer

          L 1 Reply Last reply
          0
          • M musefan

            Ok as its not too much code, though im not testing so let me know any errors

            ///<summary>
            ///Merges any characters on there own
            ///<summary>
            ///<param name="s">The string to parse</param>
            string MergeFunction(string s)
            {
            string result = s;
            int count 0;//used to count num of removed chars, this is because length of result may get shorter
            //than the length of s due to removal of spaces
            for(int i = 0; i < s.Length-1; i++)//no need to test last char so use s.length -1
            {
            if(i == 0)//if first char
            {
            if(s[i+1] == ' ')//check if next char is a space
            {
            result = result.Remove(i+1-count, 1);
            count++;
            }
            }
            else//any char other than the first and last(due to for loop limit)
            {
            if(s[i-1] == ' ' && s[i+1] == ' ')//check if both sides of char is a space
            {
            result = result.Remove(i+1-count, 1);
            count++;
            }

            }

            }

            return result;
            }

            My opinion is... If someone has already posted an answer, dont post the SAME answer

            L Offline
            L Offline
            lawrenceinba
            wrote on last edited by
            #5

            using System;
            using System.Collections;

            class chapter10
            {
            static void Main()

            ///
            ///Merges any characters on there own
            ///
            ///The string to parse
            //string MergeFunction(string s)
            {
            string result = "s y t y i";
            int count= 0;//used to count num of removed chars, this is because length of result may get shorter
            //than the length of s due to removal of spaces
            for(int i = 0; i < result.Length-1; i++)//no need to test last char so use s.length -1
            {
            if(i == 0)//if first char
            {
            if (result[i + 1] == ' ')//check if next char is a space
            {
            result = result.Remove(i+1-count, 1);
            count++;
            }
            }
            else//any char other than the first and last(due to for loop limit)
            {
            if (result[i - 1] == ' ' && result[i + 1] == ' ')//check if both sides of char is a space
            {
            result = result.Remove(i+1-count, 1);
            count++;
            }

            }

            }

            //return result;

            Console.WriteLine( result);
            }
            }

            this is my modified code.... but it checks only first and second char....wat should i do now.... pls specify changes alone in bold letter... thanks u very much for ur help

            the quieter u become more u hear

            M 1 Reply Last reply
            0
            • L lawrenceinba

              hi friends i want to implement a logic .. string input will be given...it should merge only single characters..pls help eg.. behind r m d street o/p expected is behind rmd street any body help me out with apt logic......

              the quieter u become more u hear

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              There's a one-liner for everyting. ;) text = Regex.Replace(text, @"\b([^ ]) (?=[^ ]\b)", "$1");

              Despite everything, the person most likely to be fooling you next is yourself.

              L 1 Reply Last reply
              0
              • L lawrenceinba

                using System;
                using System.Collections;

                class chapter10
                {
                static void Main()

                ///
                ///Merges any characters on there own
                ///
                ///The string to parse
                //string MergeFunction(string s)
                {
                string result = "s y t y i";
                int count= 0;//used to count num of removed chars, this is because length of result may get shorter
                //than the length of s due to removal of spaces
                for(int i = 0; i < result.Length-1; i++)//no need to test last char so use s.length -1
                {
                if(i == 0)//if first char
                {
                if (result[i + 1] == ' ')//check if next char is a space
                {
                result = result.Remove(i+1-count, 1);
                count++;
                }
                }
                else//any char other than the first and last(due to for loop limit)
                {
                if (result[i - 1] == ' ' && result[i + 1] == ' ')//check if both sides of char is a space
                {
                result = result.Remove(i+1-count, 1);
                count++;
                }

                }

                }

                //return result;

                Console.WriteLine( result);
                }
                }

                this is my modified code.... but it checks only first and second char....wat should i do now.... pls specify changes alone in bold letter... thanks u very much for ur help

                the quieter u become more u hear

                M Offline
                M Offline
                musefan
                wrote on last edited by
                #7

                lawrenceinba wrote:

                string result = "s y t y i";

                Should be :- string s = "s y t y i";//you need an original instance, not just the result instance string result = s;

                lawrenceinba wrote:

                if (result[i + 1] == ' ')//check if next char is a space

                Should be :- s[i + 1] == ' '

                lawrenceinba wrote:

                if (result[i - 1] == ' ' && result[i + 1] == ' ')//

                Should be :- s[i - 1] == ' ' && s[i + 1] == ' ' but please make it a seperate function and call it from within your main, its much better practise i.e.

                string result = MergeFunction("s y t y i");

                My opinion is... If someone has already posted an answer, dont post the SAME answer

                1 Reply Last reply
                0
                • G Guffa

                  There's a one-liner for everyting. ;) text = Regex.Replace(text, @"\b([^ ]) (?=[^ ]\b)", "$1");

                  Despite everything, the person most likely to be fooling you next is yourself.

                  L Offline
                  L Offline
                  lawrenceinba
                  wrote on last edited by
                  #8

                  input= " s y t b/d y i yt" according to ur code it give out sytb/d yi yt only single characters should be joined what should i do now if it has any delimiters it should not be joined... delimiters are (!#&*;/) thanks

                  the quieter u become more u hear

                  G 1 Reply Last reply
                  0
                  • L lawrenceinba

                    input= " s y t b/d y i yt" according to ur code it give out sytb/d yi yt only single characters should be joined what should i do now if it has any delimiters it should not be joined... delimiters are (!#&*;/) thanks

                    the quieter u become more u hear

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #9

                    Use a positive lookbehind to match a space of the beginning of the string followed by a non-space character, and look for a space or the end of the string instead of the word boundary in the lookahead.

                    Despite everything, the person most likely to be fooling you next is yourself.

                    L 1 Reply Last reply
                    0
                    • G Guffa

                      Use a positive lookbehind to match a space of the beginning of the string followed by a non-space character, and look for a space or the end of the string instead of the word boundary in the lookahead.

                      Despite everything, the person most likely to be fooling you next is yourself.

                      L Offline
                      L Offline
                      lawrenceinba
                      wrote on last edited by
                      #10

                      ya tat's wat im expecting but how to i do it using regex for tat i should use for loop.. isn't it.. is there any way in regex

                      the quieter u become more u hear

                      G 1 Reply Last reply
                      0
                      • L lawrenceinba

                        ya tat's wat im expecting but how to i do it using regex for tat i should use for loop.. isn't it.. is there any way in regex

                        the quieter u become more u hear

                        G Offline
                        G Offline
                        Guffa
                        wrote on last edited by
                        #11

                        lawrenceinba wrote:

                        ya tat's wat im expecting

                        wy ar yu takin lik dis?

                        lawrenceinba wrote:

                        but how to i do it using regex for tat i should use for loop.. isn't it.. is there any way in regex

                        Yes, there is. If you do what I already explained, it looks like this: text = Regex.Replace(text, @"(?<=^| )([^ ]) (?=[^ ](?: |$))", "$1");

                        Despite everything, the person most likely to be fooling you next is yourself.

                        L 3 Replies Last reply
                        0
                        • G Guffa

                          lawrenceinba wrote:

                          ya tat's wat im expecting

                          wy ar yu takin lik dis?

                          lawrenceinba wrote:

                          but how to i do it using regex for tat i should use for loop.. isn't it.. is there any way in regex

                          Yes, there is. If you do what I already explained, it looks like this: text = Regex.Replace(text, @"(?<=^| )([^ ]) (?=[^ ](?: |$))", "$1");

                          Despite everything, the person most likely to be fooling you next is yourself.

                          L Offline
                          L Offline
                          lawrenceinba
                          wrote on last edited by
                          #12

                          oops......i'm very sorry... i thought you could understand.....here after i'll type fully... thanks

                          the quieter u become more u hear

                          1 Reply Last reply
                          0
                          • G Guffa

                            lawrenceinba wrote:

                            ya tat's wat im expecting

                            wy ar yu takin lik dis?

                            lawrenceinba wrote:

                            but how to i do it using regex for tat i should use for loop.. isn't it.. is there any way in regex

                            Yes, there is. If you do what I already explained, it looks like this: text = Regex.Replace(text, @"(?<=^| )([^ ]) (?=[^ ](?: |$))", "$1");

                            Despite everything, the person most likely to be fooling you next is yourself.

                            L Offline
                            L Offline
                            lawrenceinba
                            wrote on last edited by
                            #13

                            again some sort of error is coming given input string b = "no 55;r m d 7"; expected output no 55; rmd 7 obtained output no 55;r md7 what should i do now.. help please how's my typing now :) :) :)

                            the quieter u become more u hear

                            G 1 Reply Last reply
                            0
                            • G Guffa

                              lawrenceinba wrote:

                              ya tat's wat im expecting

                              wy ar yu takin lik dis?

                              lawrenceinba wrote:

                              but how to i do it using regex for tat i should use for loop.. isn't it.. is there any way in regex

                              Yes, there is. If you do what I already explained, it looks like this: text = Regex.Replace(text, @"(?<=^| )([^ ]) (?=[^ ](?: |$))", "$1");

                              Despite everything, the person most likely to be fooling you next is yourself.

                              L Offline
                              L Offline
                              lawrenceinba
                              wrote on last edited by
                              #14

                              kindly send me any good link to study about patterning Regex.... what's the meaning for each characters used in that.. so and so... only i need these informations... general Regex functions and operations i know.... thanks

                              the quieter u become more u hear

                              1 Reply Last reply
                              0
                              • L lawrenceinba

                                again some sort of error is coming given input string b = "no 55;r m d 7"; expected output no 55; rmd 7 obtained output no 55;r md7 what should i do now.. help please how's my typing now :) :) :)

                                the quieter u become more u hear

                                G Offline
                                G Offline
                                Guffa
                                wrote on last edited by
                                #15

                                You have to define what you expect to happen, you change it all the time. You started off with merging single characters, and now you want to separate words by inserting extra spaces also... For example, how should the delimiters work? Should a space be added after each delimiter, and should that happen all the time or only if there is a single character after it? Does it matter if that single character will be merged with another single character? If there is a single character before a delimiter, should a space be added, and should that happen only if there is another single character to merge it with? Also, why do you expect the last character in your example to remain a single character eventhough there is another single character before it to merge it with? Perhaps you should explain what it is that you are actually going to use it for, instead of making up abstract examples. Regular expressions can be used to a lot of things, but guessing is not one of them... ;)

                                Despite everything, the person most likely to be fooling you next is yourself.

                                L 1 Reply Last reply
                                0
                                • G Guffa

                                  You have to define what you expect to happen, you change it all the time. You started off with merging single characters, and now you want to separate words by inserting extra spaces also... For example, how should the delimiters work? Should a space be added after each delimiter, and should that happen all the time or only if there is a single character after it? Does it matter if that single character will be merged with another single character? If there is a single character before a delimiter, should a space be added, and should that happen only if there is another single character to merge it with? Also, why do you expect the last character in your example to remain a single character eventhough there is another single character before it to merge it with? Perhaps you should explain what it is that you are actually going to use it for, instead of making up abstract examples. Regular expressions can be used to a lot of things, but guessing is not one of them... ;)

                                  Despite everything, the person most likely to be fooling you next is yourself.

                                  L Offline
                                  L Offline
                                  lawrenceinba
                                  wrote on last edited by
                                  #16

                                  if a delimter is before or after a single character it also should be merged together.. that's..... other things i fixed ... pls send me links regading regular expression symbols...

                                  the quieter u become more u hear

                                  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