-3

I'm a programing student, so I've started with vb.net as my first language and I need some help.

I need to know how I delete excess white spaces between words in a sentence, only using these string functions: Trim, instr, char, mid, val and len.

I made a part of the code but it doesn't work, Thanks. enter image description here

  • 'cleantext' is `Nothing` before the routine starts, nothing is not an empty string, does this not give a `NullReferenceException`? If you want to clear a `TextBox`, set its `Text` to `""` (or `String.Empty` if you prefer). – Mike Jun 04 '17 at 02:50
  • https://dotnetfiddle.net/k5UoI9 fiddle created using answer given in https://stackoverflow.com/questions/20977246/replacing-multiple-spaces-into-just-one – Bryan Dellinger Jun 04 '17 at 02:53
  • @BryanDellinger OP has stated the functions he is allowed to use. – Mike Jun 04 '17 at 02:56
  • seems rather arbitrary, why limit yourself? – Bryan Dellinger Jun 04 '17 at 03:01
  • @BryanDellinger I am assuming those parameters were set by whoever gave him the task in the first place. It may be coursework. – Mike Jun 04 '17 at 03:03
  • well if it is coursework we certainly shouldn't be doing his homework for him – Bryan Dellinger Jun 04 '17 at 03:04
  • `using these string functions: ... instr, char, mid, val and len` - Why are you using legacy VB6 functions if you are programming in VB.NET? The .NET equivalents (**which you should be using!**) are: `String.IndexOf, String.Chars(), String.Substring, Decimal.TryParse, String.Length`. – Visual Vincent Jun 04 '17 at 08:37
  • Sorry my teacher has limited me. Thank you all, I was able to solve it :) – Gabriel Mendez Jun 04 '17 at 14:01
  • Why is it that all teachers tell their students to use old functions that only exist for backwards compatibility with VB6?? Those functions have been out of date since VB.NET's release in 2002! - **That's 15 years ago!** – Visual Vincent Jun 04 '17 at 14:23
  • I just don't get teachers... I've seen many questions from users with the same kind of limitations as you here on Stack Overflow. – Visual Vincent Jun 04 '17 at 14:27
  • I found a very good link which lists all the old VB6 functions and their .NET equivalents. This could come in handy for you in the future: https://en.m.wikibooks.org/wiki/Visual_Basic_.NET/Visual_Basic_6_to_.NET_Function_Equivalents (or perhaps for your teacher? :) ) – Visual Vincent Jun 04 '17 at 14:34
  • Oh, it sounds good, thanks :) – Gabriel Mendez Jun 08 '17 at 20:05

3 Answers3

0

Knocked up a quick routine for you.

Public Function RemoveMyExcessSpaces(str As String) As String
    Dim r As String = ""
    If str IsNot Nothing AndAlso Len(str) > 0 Then
        Dim spacefound As Boolean = False
        For i As Integer = 1 To Len(str)
            If Mid(str, i, 1) = " " Then
                If Not spacefound Then
                    spacefound = True
                End If
            Else
                If spacefound Then
                    spacefound = False
                    r += " "
                End If
                r += Mid(str, i, 1)
            End If
        Next
    End If
    Return r
End Function

I think it meets your criteria.

Hope that helps.

Mike
  • 419
  • 4
  • 11
0

Unless using those VB6 methods is a requirement, here's a one-line solution:

TextBox2.Text = String.Join(" ", TextBox1.Text.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries))

Online test: http://ideone.com/gBbi55

  • String.Split() splits a string on a specific character or substring (in this case a space) and creates an array of the string parts in-between. I.e: "Hello There" -> {"Hello", "There"}

  • StringSplitOptions.RemoveEmptyEntries removes any empty strings from the resulting split array. Double spaces will create empty strings when split, thus you'll get rid of them using this option.

  • String.Join() will create a string from an array and separate each array entry with the specified string (in this case a single space).

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
-1

There is a very simple answer to this question, there is a string method that allows you to remove those "White Spaces" within a string.

Dim text_with_white_spaces as string = "Hey There!"
Dim text_without_white_spaces as string = text_with_white_spaces.Replace(" ", "")
'text_without_white_spaces should be equal to "HeyThere!"

Hope it helped!

JayCee
  • 1
  • 1
  • The problem with you answer is that it removes all spaces. The question is how to remove excess white spaces. –  Jun 04 '17 at 10:05