![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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! 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 Specs![]() |
| | #3 (permalink) |
| | 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 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||