Click here to Skip to main content
15,911,711 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
How to split the string and get specific value in the result string and also i want to remove some value

for eg:

I have a string this ->> AB , BC , .2584kG

In the above string i want to splt and in the result i want only the number value .2584 not the kg string
Posted
Updated 16-May-13 11:44am
v5
Comments
ZurdoDev 16-May-13 11:17am    
I would recommend studying the various methods on a string object.
Richard MacCutchan 16-May-13 11:58am    
The first thing you should do is tell us what programming language you are using.
lalitkr 16-May-13 12:32pm    
visual basic 6.0
Richard MacCutchan 16-May-13 12:47pm    
A very dead language.
Sergey Alexandrovich Kryukov 16-May-13 17:08pm    
Any particular reason to struggle with the language which was dead even before it was introduce to the market?
—SA

You can use Mid$(...) to extract the number if the formatting is fixed.
Use the InStr(...) and InstrRev(...) functions to find the positions of the numeric part of the string if you have specific strings that delimit the number in the string, and then use Mid$().
Tell us what the general format of the possible strings is (with widths where known) and we can try to help further.

Sorry about you being stuck in VB6.
My wife has the same problem at her job.
(Tiny company. Legacy code. No budget for rewrite.)

EDIT: revised in response to comments:
So, if the beginning of the string is always exactly "AV,BC," then it looks like extracting the number could be done with:
VB
Dim sText as String
Dim vNumber As Double
sText = "AV,BC, 0.12345kg"
vNumber = Val(Mid$(sText, 7))

This assumes you want the value of the number, not just the string.
If you really want just the text of number part of the string, then:
VB
Dim sText as String
Dim sNumber As String
sText = "AV,BC, 0.12345kg"
sNumber = Val(Mid$(sText, 7, Len(sText) - 9)) ' 9 = 7 characters from the front + 2 from the end
 
Share this answer
 
v5
Comments
lalitkr 16-May-13 17:46pm    
Yes my string is as
MyString = "AB,BC, 0.585kg"

In the above i want to extract only the number value as this value i received from the buffer , so please help me in this
Matt T Heffron 16-May-13 17:54pm    
So is it always the case that the number starts with the 7th character and goes through the 14th character?
Or does it start and then go until 2 characters from the end?
Or is it that the number starts after the 2nd comma?
Or is it that the number starts after the last comma (no matter how many)?
Or some other pattern?
These kind of differences will change the strategy for solving this...
lalitkr 17-May-13 1:42am    
Or is it that the number starts after the last comma (no matter how many)
The number can be any and also it is necassry that it is in the points it can be in whole number also i need only the digit and no weight with the number likr 15Kg or .3685Gm
so output would eb 15 or .3685
Maciej Los 20-May-13 13:50pm    
See my answer ;)
lalitkr 20-May-13 13:54pm    
Hi ,

My string is in this format

as

"AV,BC, 0.5896kg"

in this string AV and BC is fixed anf after 4 spaces weight part comes which changes accordingly to weight so in this i want to extract the weight part only
If i good remember, Split() function is avalible in VB6 and works as is described here: Split Function (Visual Basic)[^]. If it's not avalible in VB6, test it:
VB
Sub TestIt()
Dim i As Integer
Dim sText As String

sText = "AB , BC , .2584kG"
i = GetLastComma(sText)
MsgBox Val(Mid(sText, i + 1, Len(sText) - i))

sText = "AV,BC, 0.5896kg"
i = GetLastComma(sText)
MsgBox Val(Mid(sText, i + 1, Len(sText) - i))

sText = "ZX, GB, AR, OP, .123KG"
i = GetLastComma(sText)
MsgBox Val(Mid(sText, i + 1, Len(sText) - i))

End Sub

Function GetLastComma(sFindIn As String) As Integer
Dim i As Integer
Do While InStr(i + 1, sFindIn, ",", vbBinaryCompare) > 0
    i = InStr(i + 1, sFindIn, ",", vbBinaryCompare)
Loop

GetLastComma = i

End Function
 
Share this answer
 
v3
Comments
Matt T Heffron 20-May-13 13:53pm    
OP will need to remove the "units" as well.
That part ought to be easy given this start.
My 5.
Maciej Los 20-May-13 14:06pm    
Thank you, Matt.
I belive Val(string) function[^] function "removes" units ;)
If your string has 4 spaces constant then

Use Split Function

example:

VB
Dim myString AS String
Dim splitStr() AS String
myString = "AB , BC ,    .2584kG"
splitStr() = Split(myString, "    ")    'Here the 4 Spaces are given which will give you, your string
MsgBox "The String You Are Looking For Is: " & splitStr(0)


OR

You can also use this links for reference

http://www.vb6.us/tutorials/vb-string-array-functions-split-join-filter[^]

http://msdn.microsoft.com/en-us/library/6x627e5f(v=vs.90).aspx[^]

Hope this will help You...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900