Ways to aware that some string contains a substrings

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
berserker
Posts: 95
Joined: 18 Dec 2013 00:51

Re: Ways to aware that some string contains a substrings

#16 Post by berserker » 08 Jan 2014 00:21

foxidrive wrote:This is as robust as simple batch gets:

Code: Select all

@echo off
set "string1=needleb"
set "string2=aneedle"
set "string3=aneedleb"

if /i "|||%string1%"=="|||needle%string1:needle=%" echo needle is at the beginning
if /i "%string2%|||"=="%string2:needle=%needle|||" echo needle is at the end
if /i not "%string3%"=="%string3:needle=%" echo string contains needle
pause



needle is at the beginning
needle is at the end
string contains needle


nice solution. actually also depend on TS whether he needs to trim off spaces(or unseen characters in front or end) or not.

siberia-man
Posts: 208
Joined: 26 Dec 2013 09:28
Contact:

Re: Ways to aware that some string contains a substrings

#17 Post by siberia-man » 08 Jan 2014 03:13

The foxidrive suggestion is almost the best. But it doesn't work in some specific cases when NEEDLE contains "=" or "*" symbols (in addition to the existing troubles with few specific symbols). Read my first post =)

berserker
Posts: 95
Joined: 18 Dec 2013 00:51

Re: Ways to aware that some string contains a substrings

#18 Post by berserker » 08 Jan 2014 04:34

siberia-man wrote:The foxidrive suggestion is almost the best. But it doesn't work in some specific cases when NEEDLE contains "=" or "*" symbols (in addition to the existing troubles with few specific symbols). Read my first post =)

so why don't you show more on your specific cases. help us to help you. surely you can't expect others to guess what's happening right?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Ways to aware that some string contains a substrings

#19 Post by foxidrive » 08 Jan 2014 04:48

There is no foolproof method that is simple.

If you want foolproof and simple then it's time to turn to VBS at the very least :)

siberia-man
Posts: 208
Joined: 26 Dec 2013 09:28
Contact:

Re: Ways to aware that some string contains a substrings

#20 Post by siberia-man » 08 Jan 2014 07:33

berserker wrote:so why don't you show more on your specific cases. help us to help you. surely you can't expect others to guess what's happening right?

have you ever read the points I have mentioned early? I began from the description of existing solutions and shown possible issues.

foxidrive wrote:If you want foolproof and simple then it's time to turn to VBS at the very least :)

Yes. VBS is good replacement, even though I prefer writing on JS, PERL, or migrate to CYGWIN shell.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Ways to aware that some string contains a substrings

#21 Post by Squashman » 08 Jan 2014 07:48

siberia-man wrote:PERL, or migrate to CYGWIN shell.

But then your script doesn't become very portable if others need to use it.

berserker
Posts: 95
Joined: 18 Dec 2013 00:51

Re: Ways to aware that some string contains a substrings

#22 Post by berserker » 08 Jan 2014 08:30

Squashman wrote:
siberia-man wrote:PERL, or migrate to CYGWIN shell.

But then your script doesn't become very portable if others need to use it.

actually writing all these languages SHOULD be "portable", as long as there is the appropriate interpreters and the environment etc.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Ways to aware that some string contains a substrings

#23 Post by Squashman » 08 Jan 2014 08:54

berserker wrote:
Squashman wrote:
siberia-man wrote:PERL, or migrate to CYGWIN shell.

But then your script doesn't become very portable if others need to use it.

actually writing all these languages SHOULD be "portable", as long as there is the appropriate interpreters and the environment etc.

Should I quote some of your previous posts about you saying things about being smart and you know what I mean. I think you know what I mean by portable. People who use PERL and CYGWIN know they need certain things installed. Someone who comes along and sees a script written in some other scripting language other than batch on this website may try to run the script as a batch file not realizing they need something else installed to run it. That is why I like to see people post disclaimers about how their scripts need to be run if they are not native to Windows. You were originally posting all kinds of solutions when you first joined here but were not explaining that you needed perl or grep or sed or awk installed.

siberia-man
Posts: 208
Joined: 26 Dec 2013 09:28
Contact:

Re: Ways to aware that some string contains a substrings

#24 Post by siberia-man » 09 Jan 2014 00:10

Squashman wrote:But then your script doesn't become very portable if others need to use it.

yes or not - depends on tasks. The proper organization of a code could minimize influence of environment. Moreover, we could hide all differences into configuration files.

berserker
Posts: 95
Joined: 18 Dec 2013 00:51

Re: Ways to aware that some string contains a substrings

#25 Post by berserker » 09 Jan 2014 00:43

Squashman wrote:Should I quote some of your previous posts about you saying things about being smart and you know what I mean. I think you know what I mean by portable.

well then obviously we have different wave length. I am also sure you know I am not talking about that kind of "portable". Shall we stop at that, agree to disagree?

Post Reply