?
Solved

Delete spaces after end of string containing two words VB.NET 2005

Posted on 2007-07-24
14
Medium Priority
?
1,816 Views
Last Modified: 2013-11-26
I would like to take a string and if it has two words in it, I would like to strip any spaces that occur at the end of the string (something like a "trim right").  However, if it is only one word, then I would like to leave a single space after the word, if the user entered it, and get rid of anything else which occurs after.  For example:

"Hello, world   " gets converted to "Hello, world"

"Hello    " gets converted to "Hello "

How do I do this in VB.NET 2005?

Thanks.
0
Comment
Question by:amaximia
  • 4
  • 3
  • 2
  • +2
13 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 19557865
0
 
LVL 8

Expert Comment

by:sabeesh
ID: 19557878
You can check the no of spaces in a string using string function(IndexOf(' ')) and space count is grater than 2 use Trim function, otherwise add a space at the end.
0
 

Author Comment

by:amaximia
ID: 19558076
Is there any way to say if I can find in the string "[a-Z]<space>[a-Z]" then do a TrimEnd, else leave it alone?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 37

Expert Comment

by:gregoryyoung
ID: 19558125
Well you could check the string with a regexp then use an if() to check for matching the conditionally run the TrimEnd()
0
 
LVL 48

Assisted Solution

by:jpaulino
jpaulino earned 248 total points
ID: 19558392
You can count the number of word to check what you want

Dim txt As String = TextBox1.Text
Dim myArray() As String = txt.Trim.Split(" ")
Dim TotalWords = myArray.GetUpperBound(0) + 1

Then you can compare it using the IF expression and performe the trim end.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 19558504
except that would also validate 1234 5678 and "  "as applying :)

0
 
LVL 48

Expert Comment

by:jpaulino
ID: 19558643
Yes gregoryyoung but if you are looking for word and not number you can do that.
0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 252 total points
ID: 19558672
"Yes gregoryyoung but if you are looking for word and not number you can do that."

" if I can find in the string "[a-Z]<space>[a-Z]""

My point was your method of using split will give positives for other circumstances than the one mentioned.
0
 
LVL 8

Assisted Solution

by:sabeesh
sabeesh earned 252 total points
ID: 19559284
Try this

 string str = "Hello World, ";
        Regex r = new Regex(@"\s{1,}");
        Match m = r.Match(str);
       if(m.Success && (m.Index != str.Length -1))
       {
           str = str.TrimEnd();
       }
0
 

Author Comment

by:amaximia
ID: 19559540
Sabeesh,

I will, after converting to VB which should not be a problem.  Will it work under these circunstances:

"hello world" is unchanged
"hello world " (one space after) becomes "hello world" (no spaces after)
"hello " (one space after) is unchanged because it is only one word
"hello      " (several spaces after) becomes "hello " (one space after)

Thanks.
0
 
LVL 8

Expert Comment

by:sabeesh
ID: 19559723
string str = "hello      ";
        Regex r = new Regex(@"\s{1,}");
        Match m = r.Match(str.TrimEnd());
       if(m.Success )
       {
           str = str.TrimEnd();
       }

You can try this also. so this will remove space from the end and later you can add the space at the end if it is required.
0
 
LVL 2

Assisted Solution

by:sakshi_net
sakshi_net earned 248 total points
ID: 19563472
hi amaximia,

Here is the which will surely serve your purpose of

"hello world" is unchanged
"hello world " (one space after) becomes "hello world" (no spaces after)
"hello " (one space after) is unchanged because it is only one word
"hello      " (several spaces after) becomes "hello " (one space after)

----------------------------------------------------------------------------------------------------------------------
  Dim intCount As Integer

        Dim testString As String = TextBox1.Text

        MsgBox("Initial String" & testString.Length) //check the initial length of the string

// even if there is a single word or more than one word remove all the space from right end
        testString = testString.TrimEnd()

        Dim myArray() As String = testString.Split(" ")
        intCount = myArray.Length()

        If intCount = 1 Then

            testString = testString.TrimEnd()

            testString = testString & " "

        End If
        MsgBox("Final String" & testString.Length)
0
 
LVL 2

Expert Comment

by:sakshi_net
ID: 19563481
inside if condition I am checking if the entered string contains only one word then the final string will have one space added at the end
0

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question