Question Cannot remove the extra spaces from textfield output !!!!

Joined
Dec 8, 2014
Messages
5
Programming Experience
1-3
So, I am executing a function which returns some string value in textfields. But, when I am trying to use the textfield value somewhere else; it gets added with some extra spaces and I am not able to achieve the desired task. I have tried Trim() and it didnt help and I have also tried string.replace to replace the white spaces with empty but nothing worked.

VB.NET:
sb.Append("$upn= get-mailbox -Identity " + mailboxpop.TextBox1.Text + " | select *userprincipalname* ")sb.Append(vbNewLine)


Dim fchangecmd As String


Dim upn As String
upn = TextBox1.Text
upn = upn.Replace(" ", String.Empty)


 Dim fn As String
 fn = TextBox6.Text
 fn = fn.Replace(" ", String.Empty)


 fchangecmd = "Set-Msoluser -UserPrincipalName" + upn + " -FirstName " + fn


 sb.Append(fchangecmd)


 sb.AppendLine()


        Dim strFilePath = (mydocpath & "\save.txt")
        If System.IO.File.Exists(strFilePath) Then
            System.IO.File.Delete(strFilePath)
        End If
        Using outfile As New StreamWriter(mydocpath & "\save.txt")
            outfile.Write(sb.ToString())
        End Using


        strFilePath = (mydocpath & "\save.ps1")
        If System.IO.File.Exists(strFilePath) Then
            System.IO.File.Delete(strFilePath)
        End If


        System.IO.File.Move((mydocpath & "\save.txt"), (mydocpath & "\save.ps1"))
And the above code gives me the "save.ps1" file with the below text:

$upn= get-mailbox -Identity test3@r3b37.in | select *userprincipalname*
Set-Msoluser -UserPrincipalName


test3@r3b37.in






-FirstName


tetst3

Desired state: $upn= get-mailbox -Identity test3@r3b37.in | select *userprincipalname*
Set-Msoluser -UserPrincipalName test3@r3b37.in -FirstName test3
 
Last edited:

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,431
Location
Norway
Programming Experience
10+
I have tried Trim()
Depends on how you used it. It will remove all whitespace chars before and after text. 'Whitespace' includes lots of chars, not just spaces, but also linefeeds and many more.
Your TextBox1/TextBox6.Texts clearly includes linefeeds before and after. Try this:
Dim upn As String = TextBox1.Text.Trim

and same with 'fn', then use them in the fchangecmd string.

Also a coding tip about this:
sb.Append(fchangecmd)
sb.AppendLine()
is the same as this:
sb.AppendLine(fchangecmd)
 
Top Bottom