如何从特殊字符串中获取第一个单词? [英] how to get first word from special character string ?

查看:133
本文介绍了如何从特殊字符串中获取第一个单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何从特殊字符串中获取第一个字符。



for ex。我有下面的字符串,想得到第一个字'是' -







Hi,
How to get first character from special characters string.

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



~~~~~~~Yes~~~~~~~~No~No~~~~~~~~~






和另一个案例是,我想要'也许' -










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



Maybe~~~~~~~~No~No~~~~~~~~~









有什么方法吗?谢谢





Is there any approach ? Thanks

推荐答案

可能应该更具体地说明你到目前为止如何解决这个问题,但无论如何。从简单开始:

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



因为必须知道WHAT(word),所以usg PATINDEX与一些字符串操作(RIGHT,LEFT,SUBSTRING)可能是合适的。这是一些消息窗口输出的东西,当执行TSQL查询时看起来很有趣:


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]

一些可能感兴趣的更天真的功能:


[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]


[end edit]


您可以使用正则表达式执行此操作。这个问题可以用正则表达式解决...





问候,

dbg1912
You Can use the regular expression to do this. This problem can be solve using regular expression...


Regards,
dbg1912


这篇关于如何从特殊字符串中获取第一个单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆