473,394 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

String Function

25
How can I trim a string as follows?


Dim MyText AS String

MyText = "X1123-2"




so that MyText can become "X1123"
Feb 18 '08 #1
3 1929
Stewart Ross
2,545 Expert Mod 2GB
How can I trim a string as follows?
MyText = "X1123-2"
so that MyText can become "X1123"
Hi Codebug. Bit more information would be helpful here...

Is the 'extra' bit always separated by a dash, and is the dash always present? If both of these are always true, you can use InStr to find the position of the dash, then Left to extract the portion of the string before the dash, or indeed both together:

Left(MyText, InStr(MyText, "-")-1)

If it is not always a dash that separates the other part, is it any punctuation character, or one of very few (such as a period '.')?

Need to know in order to make suggestions that will work always and not just sometimes. Easy to overlook the other cases - until the function fails when faced with no dash or a different separator!

-Stewart
Feb 18 '08 #2
Codebug
25
yes It is always a '-' that seperates the text, although sometimes there is no '-' presnt and need to leave the string as it is


ie.

"X1287-2" = "X1287"
"X1287" = "X1287
Feb 18 '08 #3
Stewart Ross
2,545 Expert Mod 2GB
yes It is always a '-' that seperates the text, although sometimes there is no '-' presnt and need to leave the string as it is
ie.
"X1287-2" = "X1287"
"X1287" = "X1287
...in which case we need to test for the dash not being present:

Expand|Select|Wrap|Line Numbers
  1. Public Function BeforeDash(StringIn As String) As String
  2.     Dim DashPos As Integer
  3.     Const Separator = "-"
  4.     DashPos = InStr(1, StringIn, Separator)
  5.     If DashPos > 0 Then
  6.         BeforeDash = Left$(StringIn, DashPos - 1)
  7.     Else
  8.         BeforeDash = StringIn
  9.     End If
  10. End Function
Place this function in a general module somewhere and refer to it as you would any other function (e.g. BeforeDash([field name])

-Stewart
Feb 18 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
9
by: Danny | last post by:
HI again Is there a nifty function in access that will: 1. return the amount of occurances of a small string within a larger string? this<br>is<br>a<br>test would return 3 for <br>
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
18
by: intercom5 | last post by:
I'm writing a program in C, and thus have to use C strings. The problem that I am having is I don't know how to reallocate the space for a C string outside the scope of that string. For example:...
35
by: Cor | last post by:
Hallo, I have promised Jay B yesterday to do some tests. The subject was a string evaluation that Jon had send in. Jay B was in doubt what was better because there was a discussion in the C#...
5
by: Sia Jai Sung | last post by:
Hi, I have a class that I modify from a sample program, like below ========================================== Imports System Imports System.Web.UI Imports System.Security.Cryptography ...
9
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but...
17
by: Tom | last post by:
Is there such a thing as a CONTAINS for a string variable in VB.NET? For instance, I want to do something like the following: If strTest Contains ("A","B", "C") Then Debug.WriteLine("Found...
11
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.