StringBuilder, add Tab between Values
Asked Answered
I

5

7

I have a small problem:

I have a List of fields, with 3 Values. I want to build my String with these three Values, delimited by a "TAB"..

Code:

StringBuilder stringBuilder = new StringBuilder();
foreach (string field in fields)
{
    stringBuilder.Append(field).Append("\t");
}
stringBuilder.AppendLine();
return stringBuilder.ToString();

The Tab is only between the 3rd and 2nd Value (Between 1st and 2nd is a Space ?!)

So I tried this:

StringBuilder stringBuilder = new StringBuilder();
foreach (string field in fields)
{
    stringBuilder.Append(field + "\t").Append("\t");
}
stringBuilder.AppendLine();
return stringBuilder.ToString();

Then I have 2 Tabs between 3rd and 2nd, one Tab between 1st and 2nd and also a Space between 1st and 2nd..

(The Space is always there, how to avoid that?)

So what I have to do? Need only (without spaces..) a Tab between these Values..

Incurve answered 8/2, 2012 at 10:44 Comment(7)
How do you verify the tab is not there? By looking at where on the screen the text ends up?Lesterlesya
At least I'm taking the String (return) and I copy it to Clipboard via Code, then I paste it in the NotepadIncurve
Are you aware that Notepad sets tab stops at fixed points, and if your tab ends up close to such point, it will appear as narrow as a space?Lesterlesya
just saw that now.. Tried it then in Word and there it works.. Well Problem solved ;DIncurve
probably could do with looking in notepad++/vim or something with a binary editor to look at what you actually haveKickback
should download it first heheIncurve
hmm, notepad++ shows the same like notepad, the false things, but Word shows it well...Incurve
F
17

try

StringBuilder stringBuilder = new StringBuilder();
foreach (string field in fields)
{
    stringBuilder.Append(field.Trim()).Append("\t");
}
stringBuilder.AppendLine();
return stringBuilder.ToString();

Basically I would just use return string.Join ( "\t", fields ); .

Fugal answered 8/2, 2012 at 10:48 Comment(3)
just solved the problem, look at Question Comment, but the Idea with Trim is good, I'l mark that as answer :)Incurve
Is this platform independent ? for example, Environment.Newline is platform independent while "\n" is not.Quartzite
@blasto AFAIK there is only one character for "Tab" - so IMHO this is platform-independent...Fugal
C
2

I think you are confusing with tab char and sapces.

Are you expecting fixed no of white spaces to be added in the end of every word?

\t -> is just a tab char

The following is generated by the code given by you.

Java    StackOverflow   Banyan
Javasun StackOverflow   Banyan

The above two lines have same tab char b/w the 1st & 2nd Word.

if you type one more char in the end of "Javasun" it will extend like the following

Javaasunk   StackOverflow   Banyan
Confirmatory answered 8/2, 2012 at 10:59 Comment(0)
K
1

I would have thought you would want

StringBuilder stringBuilder = new StringBuilder();
foreach (string field in fields)
{
    stringBuilder.Append(field);
    stringBuilder.Append("\t");
}
stringBuilder.AppendLine();
return stringBuilder.ToString();
Kickback answered 8/2, 2012 at 10:47 Comment(3)
although looking at MSDN, Append returns a reference to the stringbuilder anyway so shouldn't make a difference.Kickback
it's the same.. but just solved the problem, look at Question CommentIncurve
yep, I added a comment up there tooKickback
R
1

Instead of looping, you can use string.Join:

StringBuilder stringBuilder = new StringBuilder();

stringBuilder.AppendLine(string.Join("\t", fields));

Note that you can pass in the string directly to AppendLine as well.

Roney answered 8/2, 2012 at 10:47 Comment(0)
G
0
Sub WarningWindow(ByVal content As String)
    Dim sw As New StringWriter()
    Dim hw As New HtmlTextWriter(sw)
    Dim gridHTML As String = sw.ToString().Replace("""", "'").Replace(System.Environment.NewLine, "")
    Dim sb As New StringBuilder()
    Dim a As String = "Welcomeback"
    'GridView1.RenderControl(hw)
    sb.Append("<script type = 'text/javascript'>")
    sb.Append("window.onload = new function(){")
    sb.Append("var printWin = window.open('', '', 'left=0")
    sb.Append(",top=0,width=1400,height=500,resize=yes,scrollbars =yes');")
    sb.Append("printWin.document.write(""")
    sb.Append("<INPUT Type=Button Name=Close Size=40  Value='Clsose(GSS)' onclick='self.close()'; / > ")
    sb.Append("</br><INPUT Type=Button Name=fu Size=40  Value='Alert' onclick=javascript:window.alert('" & a & "'); / > ")
    sb.Append("<INPUT Type=Text Name=gss Size=40  Value=300 ; / > ")
    sb.Append(" <P>")
    sb.Append(content)
    sb.Append(""");")
    sb.Append("printWin.focus();")
    sb.Append("printWin.show;};")
    sb.Append("</script>")
    ClientScript.RegisterStartupScript(Me.GetType(), "suvesh", sb.ToString())
End Sub
Glasshouse answered 16/8, 2018 at 13:44 Comment(1)
call it like WarningWindow("write any thing")Glasshouse

© 2022 - 2024 — McMap. All rights reserved.