Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Modify string based on a specific character

Reply
 
Old 05-10-2009   #1 (permalink)
C Wilson


 
 

Modify string based on a specific character

I would very much like to change a string in case a certain character in that
string exists. I want my script to read the string untill it finds that
specific character and if it finds it, I want my script to remove everything
to the right of that character including the character itself.
The character in question is a middle score.

So let's say I have the following string:

"Carlos-Santana"

I would like the output to be: "Carlos"

If I have the following string:

"CarlosSantana"

I want the output to be: "CarlosSantana"

So the string should be modified only in case the middle score exists in the
string.

I can use the Replace Function but how would I specify in vbscript to remove
everything up to a certain character. Do I first have to count the number of
characters up to that certain character?

Thanks!

My System SpecsSystem Spec
Old 05-10-2009   #2 (permalink)
ekkehard.horner


 
 

Re: Modify string based on a specific character

C Wilson schrieb:
Quote:

> I would very much like to change a string in case a certain character in that
> string exists. I want my script to read the string untill it finds that
> specific character and if it finds it, I want my script to remove everything
> to the right of that character including the character itself.
> The character in question is a middle score.
>
> So let's say I have the following string:
>
> "Carlos-Santana"
>
> I would like the output to be: "Carlos"
>
> If I have the following string:
>
> "CarlosSantana"
>
> I want the output to be: "CarlosSantana"
>
> So the string should be modified only in case the middle score exists in the
> string.
>
> I can use the Replace Function but how would I specify in vbscript to remove
> everything up to a certain character. Do I first have to count the number of
> characters up to that certain character?
>
> Thanks!
As you search for a specific needle (character) there is no need for
a RegExp. As VBScript has a function to find the possition a substring
(needle) in a string (haystack) - Instr() - you don't have to read
the haystack character by character. Start with this:

Dim sNeedle : sNeedle = "-"
Dim aTests : aTests = Array( _
"" _
, "no minus in here" _
, "Carlos-Santana" _
, "CarlosSantana" _
, "- minus in front" _
, "minustail -" _
, "alle-voegel-sind-schon-da" _
, "alle - voegel - sind - schon - da" _
)
Dim sTest
For Each sTest in aTests
WScript.Echo "|" & sTest & "|"
Dim nPos : nPos = InStr( sTest, sNeedle )
If 0 < nPos Then sTest = Left( sTest, nPos - 1 )
WScript.Echo "|" & sTest & "|"
WScript.Echo
Next

output:

=== removeFromChar: remove right part of string from (incl.) char ===
||
||

|no minus in here|
|no minus in here|

|Carlos-Santana|
|Carlos|

|CarlosSantana|
|CarlosSantana|

|- minus in front|
||

|minustail -|
|minustail |

|alle-voegel-sind-schon-da|
|alle|

|alle - voegel - sind - schon - da|
|alle |

=== removeFromChar: 0 done (00:00:00) ===============================

to check if InStr() works for you and whether special cases (e.g. blanks)
need further treatment.


My System SpecsSystem Spec
Old 05-10-2009   #3 (permalink)
Al Dunbar


 
 

Re: Modify string based on a specific character


"ekkehard.horner" <ekkehard.horner@xxxxxx> wrote in message
news:4a06c682$0$32665$9b4e6d93@xxxxxx-online.net...
Quote:

>C Wilson schrieb:
Quote:

>> I would very much like to change a string in case a certain character in
>> that string exists. I want my script to read the string untill it finds
>> that specific character and if it finds it, I want my script to remove
>> everything to the right of that character including the character itself.
>> The character in question is a middle score.
>>
>> So let's say I have the following string:
>>
>> "Carlos-Santana"
>>
>> I would like the output to be: "Carlos"
>>
>> If I have the following string:
>>
>> "CarlosSantana" I want the output to be: "CarlosSantana"
>>
>> So the string should be modified only in case the middle score exists in
>> the string.
>>
>> I can use the Replace Function but how would I specify in vbscript to
>> remove everything up to a certain character. Do I first have to count the
>> number of characters up to that certain character?
>>
>> Thanks!
>
> As you search for a specific needle (character) there is no need for
> a RegExp. As VBScript has a function to find the possition a substring
> (needle) in a string (haystack) - Instr() - you don't have to read
> the haystack character by character. Start with this:
>
> Dim sNeedle : sNeedle = "-"
> Dim aTests : aTests = Array( _
> "" _
> , "no minus in here" _
> , "Carlos-Santana" _
> , "CarlosSantana" _
> , "- minus in front" _
> , "minustail -" _
> , "alle-voegel-sind-schon-da" _
> , "alle - voegel - sind - schon - da" _
> )
> Dim sTest
> For Each sTest in aTests
> WScript.Echo "|" & sTest & "|"
> Dim nPos : nPos = InStr( sTest, sNeedle )
> If 0 < nPos Then sTest = Left( sTest, nPos - 1 )
> WScript.Echo "|" & sTest & "|"
> WScript.Echo
> Next
I replaced these lines:

Dim nPos : nPos = InStr( sTest, sNeedle )
If 0 < nPos Then sTest = Left( sTest, nPos - 1 )

with this line:

if sTest <> "" then sTest = split(sTest,"-")(0)

and got identical results. One person's way to skin a cat is not
another's... ;-)

/Al
Quote:

>
> output:
>
> === removeFromChar: remove right part of string from (incl.) char ===
> ||
> ||
>
> |no minus in here|
> |no minus in here|
>
> |Carlos-Santana|
> |Carlos|
>
> |CarlosSantana|
> |CarlosSantana|
>
> |- minus in front|
> ||
>
> |minustail -|
> |minustail |
>
> |alle-voegel-sind-schon-da|
> |alle|
>
> |alle - voegel - sind - schon - da|
> |alle |
>
> === removeFromChar: 0 done (00:00:00) ===============================
>
> to check if InStr() works for you and whether special cases (e.g. blanks)
> need further treatment.
>
>

My System SpecsSystem Spec
Old 05-10-2009   #4 (permalink)
Cary Shultz


 
 

Re: Modify string based on a specific character

All the birds are already there? Shoot! I missed it!

I like the way you explain things. I am learning this stuff and your
explanations are helpful!

Cary

"ekkehard.horner" <ekkehard.horner@xxxxxx> wrote in message
news:4a06c682$0$32665$9b4e6d93@xxxxxx-online.net...
Quote:

>C Wilson schrieb:
Quote:

>> I would very much like to change a string in case a certain character in
>> that string exists. I want my script to read the string untill it finds
>> that specific character and if it finds it, I want my script to remove
>> everything to the right of that character including the character itself.
>> The character in question is a middle score.
>>
>> So let's say I have the following string:
>>
>> "Carlos-Santana"
>>
>> I would like the output to be: "Carlos"
>>
>> If I have the following string:
>>
>> "CarlosSantana" I want the output to be: "CarlosSantana"
>>
>> So the string should be modified only in case the middle score exists in
>> the string.
>>
>> I can use the Replace Function but how would I specify in vbscript to
>> remove everything up to a certain character. Do I first have to count the
>> number of characters up to that certain character?
>>
>> Thanks!
>
> As you search for a specific needle (character) there is no need for
> a RegExp. As VBScript has a function to find the possition a substring
> (needle) in a string (haystack) - Instr() - you don't have to read
> the haystack character by character. Start with this:
>
> Dim sNeedle : sNeedle = "-"
> Dim aTests : aTests = Array( _
> "" _
> , "no minus in here" _
> , "Carlos-Santana" _
> , "CarlosSantana" _
> , "- minus in front" _
> , "minustail -" _
> , "alle-voegel-sind-schon-da" _
> , "alle - voegel - sind - schon - da" _
> )
> Dim sTest
> For Each sTest in aTests
> WScript.Echo "|" & sTest & "|"
> Dim nPos : nPos = InStr( sTest, sNeedle )
> If 0 < nPos Then sTest = Left( sTest, nPos - 1 )
> WScript.Echo "|" & sTest & "|"
> WScript.Echo
> Next
>
> output:
>
> === removeFromChar: remove right part of string from (incl.) char ===
> ||
> ||
>
> |no minus in here|
> |no minus in here|
>
> |Carlos-Santana|
> |Carlos|
>
> |CarlosSantana|
> |CarlosSantana|
>
> |- minus in front|
> ||
>
> |minustail -|
> |minustail |
>
> |alle-voegel-sind-schon-da|
> |alle|
>
> |alle - voegel - sind - schon - da|
> |alle |
>
> === removeFromChar: 0 done (00:00:00) ===============================
>
> to check if InStr() works for you and whether special cases (e.g. blanks)
> need further treatment.
>
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
read text file - but starting at a specific point (not the very first character of the very first line) VB Script
Replace Nth character in a string VB Script
Getting a combined string based upon similar reference values PowerShell
Cannot delete/view/modify specific filetype Vista performance & maintenance
How do I escape the wildcard character in a path string? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46