久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

字符串替換字符串的特定部分

String replace specific part of string(字符串替換字符串的特定部分)
本文介紹了字符串替換字符串的特定部分的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有這樣的文字

Key:Value
Key2:Value2
Key3:Value3

i have
a very 
long
uncommom
text which i need to drop

Key4:Valu4

第二個例子是

Key:Value
Key2:Value2
Key3:Value3
i have a very long uncommom text which i need to drop

Key4:Valu4

重點是去掉句子

i have a very long uncommom text which i need to drop

來自這兩個 SQL 字符串.我需要提到在包含 CHAR(10)CHAR(13) 里面.

from those two SQL strings. I need to mention in contains CHAR(10) and CHAR(13) inside.

我需要在鍵值對上保留新行.

I need to keep new lines on key:value pairs.

如何實現這一目標?

這行不通.

Replace(@string,'i have a very long uncommom text which i need to drop
','')

還有什么辦法可以用一個替換雙/三/四新換行符?

Also is there any way to replace double/triple/quadruple new line breaks with just one?

推薦答案

以下代碼演示了一種將字符串拆分為單獨行并重新組合包含冒號的行的方法.它保持輸入行的順序并且不需要任何最近的功能,例如 String_Split.

The following code demonstrates one way of splitting the string into separate lines and reassembling the lines that contain a colon. It maintains the order of the input lines and does not require any recent features, e.g String_Split.

-- Sample data.
declare @Newline as VarChar(2) = Char(10) + Char(13);
declare @Sample as VarChar(1024) =
  'Key:Value' + @Newline +
  'Key2:Value2' + @Newline +
  'Key3:Value3' + @Newline +
  @Newline +
  'i have' + @Newline +
  'a very ' + @Newline +
  'long ' + @Newline +
  'uncommom' + @Newline +
  'text which i need to drop' + @Newline +
  @Newline +
  'Key4:Valu4';

-- Display the sample data.
select @Sample as Sample, Replace( @Sample, @Newline, '?' ) as VisibleSample;

-- Test the splitter.
select ItemNumber, Item, Replace( Item, @Newline, '?' ) as VisibleItem
  from dbo.DelimitedSplit8K( @Sample, @Newline )
  where Item like '%:%';

-- Reassemble the items that include a colon.
declare @Result as NVarChar(1024) = Stuff(
  ( select @Newline + Item
      from dbo.DelimitedSplit8K( @Sample, @Newline )
      where Item like '%:%'
      order by ItemNumber for XML path(''), type).value('.[1]', 'VarChar(max)' ),
    1, Len( @NewLine ), '' );
select @Result as Result, Replace( @Result, @Newline, '?' ) as VisibleResult;

拆分器是Jeff Moden's 處理可變長度分隔符的代碼:

The splitter is a modified version of Jeff Moden's code which handles a variable length delimiter:

CREATE FUNCTION [dbo].[DelimitedSplit8K]
--===== Define I/O parameters
        (@pString VARCHAR(8000), @pDelimiter VARCHAR(16))
--WARNING!!! DO NOT USE MAX DATA-TYPES HERE!  IT WILL KILL PERFORMANCE!
RETURNS TABLE WITH SCHEMABINDING AS
 RETURN
--===== "Inline" CTE Driven "Tally Table" produces values from 1 up to 10,000...
     -- enough to cover VARCHAR(8000)
  WITH E1(N) AS (
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
                ),                          --10E+1 or 10 rows
       E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
       E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
 cteTally(N) AS (--==== This provides the "base" CTE and limits the number of rows right up front
                     -- for both a performance gain and prevention of accidental "overruns"
                 SELECT TOP (ISNULL(DATALENGTH(@pString),0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
                ),
cteStart(N1) AS (--==== This returns N+1 (starting position of each "element" just once for each delimiter)
                 SELECT 1 UNION ALL
                 SELECT t.N+ Len( @pDelimiter ) FROM cteTally t WHERE SUBSTRING(@pString,t.N, Len( @pDelimiter ) ) = @pDelimiter
                ),
cteLen(N1,L1) AS(--==== Return start and length (for use in substring)
                 SELECT s.N1,
                        ISNULL(NULLIF(CHARINDEX(@pDelimiter,@pString,s.N1),0)-s.N1 ,8000)
                   FROM cteStart s
                )
--===== Do the actual split. The ISNULL/NULLIF combo handles the length for the final element when no delimiter is found.
 SELECT ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1),
        Item       = SUBSTRING(@pString, l.N1, l.L1)
   FROM cteLen l
;

這篇關于字符串替換字符串的特定部分的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Modify Existing decimal places info(修改現有小數位信息)
The correlation name #39;CONVERT#39; is specified multiple times(多次指定相關名稱“CONVERT)
T-SQL left join not returning null columns(T-SQL 左連接不返回空列)
remove duplicates from comma or pipeline operator string(從逗號或管道運算符字符串中刪除重復項)
Change an iterative query to a relational set-based query(將迭代查詢更改為基于關系集的查詢)
concatenate a zero onto sql server select value shows 4 digits still and not 5(將零連接到 sql server 選擇值仍然顯示 4 位而不是 5)
主站蜘蛛池模板: 欧美精品日韩精品 | 91精品国产高清一区二区三区 | 97国产一区二区 | 国产原创在线观看 | 亚洲欧美中文字幕 | 亚洲精品一区二区三区蜜桃久 | 国产精品久久 | 日韩中文一区二区三区 | 日韩中文字幕视频在线观看 | 中文字幕伊人 | 成人一区二区三区 | 色综合久 | 91精品一区二区三区久久久久久 | 人人九九精 | 日韩免费av网站 | 亚洲一区二区三区高清 | 中文字幕一区在线观看视频 | 在线观看中文字幕亚洲 | 亚洲精品日韩一区二区电影 | 国产欧美精品一区二区色综合 | 一区二区久久 | 国产亚洲成av人在线观看导航 | 九九免费观看视频 | 午夜看片网站 | 成人免费看黄网站在线观看 | 精品毛片视频 | 国产最新精品视频 | 91极品欧美视频 | www国产亚洲精品 | 亚洲精品9999 | 日韩av免费在线电影 | 欧美日本在线观看 | 97超级碰碰 | 国产一区不卡 | 密色视频| 99久久精品国产一区二区三区 | 亚洲成人福利视频 | 欧美女优在线观看 | 国产日韩精品视频 | 久久久久网站 | 五月婷婷 六月丁香 |