concateing strings in c#
-
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
-
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
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
-
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
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
-
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
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
-
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
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
-
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
-
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
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
-
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.
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
-
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
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.
-
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.
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
-
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
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.
-
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.
oops......i'm very sorry... i thought you could understand.....here after i'll type fully... thanks
the quieter u become more u hear
-
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.
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
-
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.
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
-
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
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.
-
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.
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