Maintain whitespace between strings
-
Hello friends, I tried converting lower case char to upper case and vice versa it works fine with the following code Dim abc As String Dim xyz As String = String.Empty Dim a As Char Console.WriteLine("Enter The String") abc = Console.ReadLine() For Each a In abc Dim b As Char If Asc(a) > 64 And Asc(a) < 91 Then b = Chr(Asc(a) + 32) ElseIf Asc(a) > 96 And Asc(a) < 123 Then b = Chr(Asc(a) - 32) End If xyz = xyz + b Next Console.WriteLine(xyz) Console.ReadLine() case 1 Input : ABCdef Output: abcDEF case 2 Input :ABC def GHI Output:abccDEFFFghi Desired Output:abc DEF ghi The issue is maintaining the whitespace between the strings, with the current code the the whitespaces are replaced by the previous charcter in the output. I like the whitespaces between the strings to be maintained . Thanks In Advance Praveen
-
Hello friends, I tried converting lower case char to upper case and vice versa it works fine with the following code Dim abc As String Dim xyz As String = String.Empty Dim a As Char Console.WriteLine("Enter The String") abc = Console.ReadLine() For Each a In abc Dim b As Char If Asc(a) > 64 And Asc(a) < 91 Then b = Chr(Asc(a) + 32) ElseIf Asc(a) > 96 And Asc(a) < 123 Then b = Chr(Asc(a) - 32) End If xyz = xyz + b Next Console.WriteLine(xyz) Console.ReadLine() case 1 Input : ABCdef Output: abcDEF case 2 Input :ABC def GHI Output:abccDEFFFghi Desired Output:abc DEF ghi The issue is maintaining the whitespace between the strings, with the current code the the whitespaces are replaced by the previous charcter in the output. I like the whitespaces between the strings to be maintained . Thanks In Advance Praveen
Member 4514218 wrote:
If Asc(a) > 64 And Asc(a) < 91 Then b = Chr(Asc(a) + 32)
What an ugly, ugly mess. Char.IsAlpha is what you need, from memory. And Char.ToLower. How about abc = abc.ToLower()
Member 4514218 wrote:
If Asc(a) > 64 And Asc(a) < 91 Then b = Chr(Asc(a) + 32) ElseIf Asc(a) > 96 And Asc(a) < 123 Then b = Chr(Asc(a) - 32) End If xyz = xyz + b
The core flaw in your logic is that you need another else. else b = a Otherwise, anyting that is not a lower or uppercase character, is lost. you look like you're coming from VB6, that's where all the nasty habits are coming from. Forget VB6, learn to use VB.NET properly. And, learn to use the debugger, if you stepped through this code, you'd have seen in no time what the issue was.
Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp
-
Hello friends, I tried converting lower case char to upper case and vice versa it works fine with the following code Dim abc As String Dim xyz As String = String.Empty Dim a As Char Console.WriteLine("Enter The String") abc = Console.ReadLine() For Each a In abc Dim b As Char If Asc(a) > 64 And Asc(a) < 91 Then b = Chr(Asc(a) + 32) ElseIf Asc(a) > 96 And Asc(a) < 123 Then b = Chr(Asc(a) - 32) End If xyz = xyz + b Next Console.WriteLine(xyz) Console.ReadLine() case 1 Input : ABCdef Output: abcDEF case 2 Input :ABC def GHI Output:abccDEFFFghi Desired Output:abc DEF ghi The issue is maintaining the whitespace between the strings, with the current code the the whitespaces are replaced by the previous charcter in the output. I like the whitespaces between the strings to be maintained . Thanks In Advance Praveen
very simple...just add one 'Else'... as below...! Dim abc As String Dim xyz As String = String.Empty Dim a As Char Console.WriteLine("Enter The String") abc = Console.ReadLine() For Each a In abc Dim b As Char If Asc(a) > 64 And Asc(a) < 91 Then b = Chr(Asc(a) + 32) ElseIf Asc(a) > 96 And Asc(a) < 123 Then b = Chr(Asc(a) - 32) Else b = a End If xyz = xyz + b Next Console.WriteLine(xyz) Console.ReadLine() Enjoy!
-
Hello friends, I tried converting lower case char to upper case and vice versa it works fine with the following code Dim abc As String Dim xyz As String = String.Empty Dim a As Char Console.WriteLine("Enter The String") abc = Console.ReadLine() For Each a In abc Dim b As Char If Asc(a) > 64 And Asc(a) < 91 Then b = Chr(Asc(a) + 32) ElseIf Asc(a) > 96 And Asc(a) < 123 Then b = Chr(Asc(a) - 32) End If xyz = xyz + b Next Console.WriteLine(xyz) Console.ReadLine() case 1 Input : ABCdef Output: abcDEF case 2 Input :ABC def GHI Output:abccDEFFFghi Desired Output:abc DEF ghi The issue is maintaining the whitespace between the strings, with the current code the the whitespaces are replaced by the previous charcter in the output. I like the whitespaces between the strings to be maintained . Thanks In Advance Praveen
Dim ca As Char() = TextBox1.Text.ToCharArray
TextBox1.Text = ""
For Each c As String In ca
Select Case Char.IsUpper(c)
Case True
TextBox1Text &= c.ToLower
Case False
TextBox1.Text &= c.ToUpper
Case Else
TextBox1.Text &= c
End Select
NextMy advice is free, and you may get what you paid for.
-
Hello friends, I tried converting lower case char to upper case and vice versa it works fine with the following code Dim abc As String Dim xyz As String = String.Empty Dim a As Char Console.WriteLine("Enter The String") abc = Console.ReadLine() For Each a In abc Dim b As Char If Asc(a) > 64 And Asc(a) < 91 Then b = Chr(Asc(a) + 32) ElseIf Asc(a) > 96 And Asc(a) < 123 Then b = Chr(Asc(a) - 32) End If xyz = xyz + b Next Console.WriteLine(xyz) Console.ReadLine() case 1 Input : ABCdef Output: abcDEF case 2 Input :ABC def GHI Output:abccDEFFFghi Desired Output:abc DEF ghi The issue is maintaining the whitespace between the strings, with the current code the the whitespaces are replaced by the previous charcter in the output. I like the whitespaces between the strings to be maintained . Thanks In Advance Praveen
Member 4514218 wrote:
Dim b As Char
replace this by
Dim b As Char = a
the logic is: output char equals input char, except when it is an upper/lowercase letter. And please use PRE tags to keep code readable. :)Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
very simple...just add one 'Else'... as below...! Dim abc As String Dim xyz As String = String.Empty Dim a As Char Console.WriteLine("Enter The String") abc = Console.ReadLine() For Each a In abc Dim b As Char If Asc(a) > 64 And Asc(a) < 91 Then b = Chr(Asc(a) + 32) ElseIf Asc(a) > 96 And Asc(a) < 123 Then b = Chr(Asc(a) - 32) Else b = a End If xyz = xyz + b Next Console.WriteLine(xyz) Console.ReadLine() Enjoy!
Thanks binjafar It is simple and effective Regards Praveen
-
Thanks binjafar It is simple and effective Regards Praveen