I want to remove unwanted blank lines and white spaces from a string.
Sample Snippet
String RemoveLines= "world usa
europe
china
";
Output Needed:
String Removelines="world usa
europe
china;
Say for example you have 8 blank lines between 1st and 10th line and want to remove 7 blank lines and keep just one blank line. Same with white spaces, just want to keep single white space.
Click "Mark as Answer" on the post that helped you.
This earns you a point and marks your thread as Resolved so we will all know you have been helped.
FAQ on the correct forum http://forums.asp.net/p/1337412/2699239.aspx#2699239
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
thanks yar... I thought that it 'll work only for character and not for string... Just now came to know it 'll work for string also.... Thanks for ur reply....
EkRaghu
Member
17 Points
59 Posts
Remove blank lines and white spaces from a string?
Dec 12, 2008 02:42 PM|LINK
I want to remove unwanted blank lines and white spaces from a string.
Sample Snippet
String RemoveLines= "world usa
europe
china
";
Output Needed:
String Removelines="world usa
europe
china;
Say for example you have 8 blank lines between 1st and 10th line and want to remove 7 blank lines and keep just one blank line. Same with white spaces, just want to keep single white space.
ldechent
Contributor
6326 Points
1577 Posts
Re: Remove blank lines and white spaces from a string?
Dec 12, 2008 04:56 PM|LINK
You can trim excess white spaces from a string
Example: mystring =" something "
with .Trim()
mystring.Trim() = "something"
If you want a trailing white space you could add it on after trimming it.
-Larry
hs_jha
Contributor
2364 Points
590 Posts
Re: Remove blank lines and white spaces from a string?
Dec 12, 2008 05:10 PM|LINK
how bout taking input with start and end black space trimmed.
for that matter you only need to use
asp.net consultant
TATWORTH
All-Star
72415 Points
14017 Posts
MVP
Re: Remove blank lines and white spaces from a string?
Dec 16, 2008 07:20 AM|LINK
How about
while (RemoveLines.Instr(" ") > -1)
{
RemoveLines = RemoveLines.Replace(" "," ")
}
while (RemoveLines.Instr("\n\n") > -1)
{
RemoveLines = RemoveLines.Replace("\n\n","\n")
}
This will of course need to be unit tested.
This earns you a point and marks your thread as Resolved so we will all know you have been helped.
FAQ on the correct forum http://forums.asp.net/p/1337412/2699239.aspx#2699239
imran_ku07
All-Star
45785 Points
7698 Posts
MVP
Re: Remove blank lines and white spaces from a string?
Dec 16, 2008 07:46 AM|LINK
this won't work without @ sign.
By the way
Check this work's String1.Replace(" "," ");
OR
replace 2 space with one or check that if previous charecter and next charecter is space then not add this like
String NewString="";
for(int i=0;i<String1.Length;i++)
{
if(i+1<String1.Length) //check next charecter is present
{
if((String1[i]=' ')&&(String1[i+1]=' '))
{
continue;
}
else
NewString+=String[i].Tostring();
}
else
NewString+=String[i].Tostring();
}
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
sirdneo
All-Star
15171 Points
2509 Posts
Re: Remove blank lines and white spaces from a string?
Dec 17, 2008 09:54 AM|LINK
Check out this simple fuction which does the trick for you:-
private string process(string s)
{
int len = s.Length;
int current=0;
StringBuilder sb = new StringBuilder(len);
while (current < len-1)
{
if (!(s[current] == ' ' && s[current + 1] == ' ') &&
!(s[current] == '\n' && s[current + 1] == '\n')
)
{
sb.Append(s[current]);
}
current++;
}
return sb.ToString();
}
string manipulation
Zeeshan Umar
~Please Mark As Answer, one or multiple posts, which helped you. So that it might be useful for others~
Srinivasan.S
Member
8 Points
4 Posts
Re: Remove blank lines and white spaces from a string?
Dec 22, 2008 04:01 AM|LINK
I've String s="aero plane";
the expected output is "aeroplane"..
is this possible to remove the blank spaces present in the middle?
Thanks in advance....
imran_ku07
All-Star
45785 Points
7698 Posts
MVP
Re: Remove blank lines and white spaces from a string?
Dec 22, 2008 04:06 AM|LINK
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Srinivasan.S
Member
8 Points
4 Posts
Re: Remove blank lines and white spaces from a string?
Dec 22, 2008 04:53 AM|LINK
thanks yar... I thought that it 'll work only for character and not for string... Just now came to know it 'll work for string also.... Thanks for ur reply....
rami_nassar
Contributor
3608 Points
828 Posts
Re: Remove blank lines and white spaces from a string?
Dec 22, 2008 05:08 AM|LINK
try this
while (RemoveLines.Instr(" ") > -1) { RemoveLines = RemoveLines.Replace(" "," ") } while (RemoveLines.Instr("\n\n") > -1) { RemoveLines = RemoveLines.Replace("\n\n","\n") }Nassar, Rami (MCP, MCTS, MCPD)
My Blog || E-Mail
Don't forget to click "Mark as Answer" on the post that helped you.