Click here to Skip to main content
15,921,716 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
How to get first character from special characters string.

for ex. i have below string and want to get first word 'Yes' -



Objective-C
~~~~~~~Yes~~~~~~~~No~No~~~~~~~~~



and also another case is , where i want 'Maybe' -



Objective-C
Maybe~~~~~~~~No~No~~~~~~~~~




Is there any approach ? Thanks
Posted
Updated 14-Nov-13 19:26pm
v2

Probably should be more specific about how you've gone about this so far, but anyway. Starting simple:
DECLARE @strItem [nvarchar](12)
SET @strItem = '%Yes%'

DECLARE @strInput [nvarchar](123)
SET @strInput = '~~~~~~~Yes~~~~~~~~No~No~~~~~~~~~'

SELECT PATINDEX(@strItem, @strInput) As [whereinStr]

Gets:
whereinStr
~~~~~~~~~~
8

Since WHAT ("word") has to be known, usg PATINDEX in conjunction with some of the string operations (RIGHT, LEFT, SUBSTRING) might be appropriate. Here's some "Message Window" output stuff which looks interesting when, as a TSQL query, is executed:
DECLARE @strUnknown [nvarchar](900)
SET @strUnknown = '~~~~~~~Yes~~~~~~~~No~No~~~~~~~~~'
DECLARE @intSpecial [int]
SET @intSpecial = 0
DECLARE @strTemp [nvarchar](270)
SET @strTemp = 'Maybe~~~~~~~~No~No~~~~~~~~~'
DECLARE @intDelimit [int]
SET @intDelimit = PATINDEX('%~%',@strUnknown)
DECLARE @intDel [int]
SET @intDel = PATINDEX('%~%',@strTemp)
DECLARE @strEatIt [nvarchar](256)
SET @strEatIt = ''
DECLARE @intLoop [int]
SET @intLoop = 1

SET @intSpecial = LEN(@strUnknown)
WHILE @intLoop < @intSpecial
	BEGIN
		--PRINT @strUnknown
		SET @strEatIt = (SELECT  RIGHT(@strUnknown,LEN(@strUnknown)-1))
		SET @strUnknown = @strEatIt
		IF LEFT(@strUnknown,1) != '~'
                  PRINT 'to consume: ' + CAST(LEN(@strUnknown) AS [nvarchar]) + ' starting with this: ' + @strUnknown + ' the index is: ' + CAST(@intLoop AS [nvarchar])
                SET @intLoop = @intLoop + 1
        END

Gets:
to consume: 25 starting with this: Yes~~~~~~~~No~No~~~~~~~~~ the index is: 7
to consume: 24 starting with this: es~~~~~~~~No~No~~~~~~~~~ the index is: 8
to consume: 23 starting with this: s~~~~~~~~No~No~~~~~~~~~ the index is: 9
to consume: 14 starting with this: No~No~~~~~~~~~ the index is: 18
to consume: 13 starting with this: o~No~~~~~~~~~ the index is: 19
to consume: 11 starting with this: No~~~~~~~~~ the index is: 21
to consume: 10 starting with this: o~~~~~~~~~ the index is: 22

[edit]
Some more naive functionality which might be of interest:
CREATE FUNCTION fn_EZ_countWord
(
    @strInput [nvarchar](1215),
    @strWord [nvarchar](20)
)
RETURNS INT
AS
	BEGIN
		RETURN (LEN(@strInput)-LEN(REPLACE(@strInput,@strWord,'')))/LEN(@strWord)
	END

Exercising:
DECLARE @strInput [nvarchar](123)
SET @strInput = 'Maybe~~~~~~~~No~No~~~~~~~~~'

SELECT [fn_EZ_SO_countOccurancesOfString](@strInput, 'Maybe') -- 1 
SELECT [fn_EZ_SO_countOccurancesOfString](@strInput, '~')     -- 18

[end edit]
 
Share this answer
 
v2
You Can use the regular expression to do this. This problem can be solve using regular expression...


Regards,
dbg1912
 
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