如何在给定的包含空格b/n的字符串中找到最大的子字符串.
问题描述:
如果我的字符串是:如何找到最大的子字符串"
我的输出应该是:子字符串中最大的是->子串
我已经创建了一个过程,它可以正常工作,但是在字符串的末尾需要一个空格.
在此先感谢您是否建议任何可以覆盖此过程的过程以获取所需的输出.
If my string is: "hi how to find the largest substring"
My output should be: The largest of the substring is--> substring
I have created a procedure, it works fine but it requires a space at the end of my string.
Thanks in advance if u suggest any procedure that could override this procedure to get the required output.
alter procedure largerSubstring @baseString varchar(250)
AS
begin
declare @subString varchar(max),@len int,@largestSubString varchar(max),@prevLength int,@originalLength int
set @prevLength=0
set @originalLength=len(@baseString)
while(@originalLength>0)
BEGIN
set @subString=left(@baseString,charindex(' ',@baseString)-1)
if(len(@subString)>@prevLength)
begin
set @prevLength=len(@subString)
set @largestSubString=@subString;
end
set @baseString=substring(@baseString,charindex(' ',@baseString)+1,@originalLength)
set @originalLength=@originalLength-(len(@subString)+1)
END
print 'the largest string length is--> '+convert(varchar,@prevLength)
print 'the largest susstring is-->'+@largestSubString
end
答
试试这个
Try this
alter procedure largerSubstring
@baseString varchar(250)
AS
begin
declare @subString varchar(max),
@len int,
@largestSubString varchar(max),
@prevLength int,
@foundIndex int
set @baseString = isnull(rtrim(ltrim(@baseString)),'')
set @prevLength = 0
set @largestSubString = @baseString
while(len(@baseString)>0)
begin
set @foundIndex = charindex(' ',@baseString)
if @foundIndex = 0 -- not found
begin
set @subString = @baseString
set @baseString = ''
end
else
begin
set @subString = left(@baseString,@foundIndex-1)
set @baseString = ltrim(right(@baseString,len(@baseString)-@foundIndex))
end
if(len(@subString)>@prevLength)
begin
set @prevLength = len(@subString)
set @largestSubString = @subString;
end
end
print 'the largest string length is--> '+convert(varchar,@prevLength)
print 'the largest susstring is-->'+@largestSubString
end
这是另一个版本,
Hi,
Here is the another version,
CREATE PROCEDURE [dbo].[usp_GetLengthyString]
(
@sInputString VARCHAR(8000)
) AS
BEGIN
DECLARE @sItem VARCHAR(8000)
DECLARE @lengthyString VARCHAR(8000)
DECLARE @sDelimiter VARCHAR
SET @sDelimiter = ' '
DECLARE @List TABLE (string VARCHAR(8000), leng INT)
WHILE CHARINDEX(@sDelimiter,@sInputString,0) <> 0
BEGIN
SELECT @sItem=RTRIM(LTRIM(SUBSTRING(@sInputString,1,CHARINDEX(@sDelimiter,@sInputString,0)-1))),
@sInputString=RTRIM(LTRIM(SUBSTRING(@sInputString,CHARINDEX(@sDelimiter,@sInputString,0)+LEN(@sDelimiter),LEN(@sInputString))))
IF LEN(@sItem) > 0
INSERT INTO @List
SELECT @sItem, LEN(@sItem)
END
IF LEN(@sInputString) > 0
INSERT INTO @List
SELECT @sInputString, LEN(@sInputString) -- Put the last item in
SELECT @lengthyString = string FROM @List ORDER BY leng ASC
PRINT 'The longest sub string is ' + @lengthyString
END
嘿,
如果U在字符串末尾需要空格,然后按Alt + 255,则插入空白字符.
祝你好运
Hey,
if U Need Space at end of string then press Alt+255 it inserted blank Character.
Good Luck