截取字符串函数

截取字符串函数:解决了中文与英文截取不同的问题。
ContractedBlock.gif ExpandedBlockStart.gif Code
 1ContractedBlock.gifExpandedBlockStart.gif 字符串截取函数#region 字符串截取函数
 2InBlock.gif    public static string CutString(string inputString, int len)
 3ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 4InBlock.gif
 5InBlock.gif        ASCIIEncoding ascii = new ASCIIEncoding();
 6InBlock.gif        int tempLen = 0;
 7InBlock.gif        string tempString = "";
 8InBlock.gif        byte[] s = ascii.GetBytes(inputString);
 9InBlock.gif        for (int i = 0; i < s.Length; i++)
10ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
11InBlock.gif            if ((int)s[i] == 63)
12ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
13InBlock.gif                tempLen += 2;
14ExpandedSubBlockEnd.gif            }

15InBlock.gif            else
16ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
17InBlock.gif                tempLen += 1;
18ExpandedSubBlockEnd.gif            }

19InBlock.gif
20InBlock.gif            try
21ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
22InBlock.gif                tempString += inputString.Substring(i, 1);
23ExpandedSubBlockEnd.gif            }

24InBlock.gif            catch
25ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
26InBlock.gif                break;
27ExpandedSubBlockEnd.gif            }

28InBlock.gif
29InBlock.gif            if (tempLen > len)
30InBlock.gif                break;
31ExpandedSubBlockEnd.gif        }

32InBlock.gif        byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString);
33InBlock.gif        if (mybyte.Length > len)
34InBlock.gif            tempString += "";
35InBlock.gif
36InBlock.gif        return tempString;
37ExpandedSubBlockEnd.gif    }

38ExpandedBlockEnd.gif    #endregion
Repeat中调用该函数:
ContractedBlock.gif ExpandedBlockStart.gif Code
1None.gif  <asp:Repeater ID="Repeater1" runat="server">
2None.gif              <ItemTemplate>
3None.gif                <a href='<%# "Bulletin/Details.aspx?BulletinID="+ DataBinder.Eval(Container.DataItem,"ID") %>'><%# Warning.CutString(DataBinder.Eval(Container.DataItem, "Title").ToString(),22)%></a>
4None.gif              </ItemTemplate>
5None.gif              </asp:Repeater>

去除最后那个“|”字符

ContractedBlock.gif ExpandedBlockStart.gif Code
 1None.gif    private string ClearLastChar(string str)
 2ExpandedBlockStart.gifContractedBlock.gif    dot.gif{
 3InBlock.gif        if (str == null)
 4ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 5InBlock.gif            return str;
 6ExpandedSubBlockEnd.gif        }

 7InBlock.gif        else
 8ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 9InBlock.gif            if (str.Length > 0)
10ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
11InBlock.gif                return str.Substring(0, str.LastIndexOf("|"));
12ExpandedSubBlockEnd.gif            }

13InBlock.gif            else
14ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
15InBlock.gif                return "";
16ExpandedSubBlockEnd.gif            }

17InBlock.gif
18ExpandedSubBlockEnd.gif        }

19ExpandedBlockEnd.gif    }

20None.gif
### 回答1: 在 Oracle 数据库中,可以使用 `SUBSTR` 函数截取字符串。该函数的语法如下: ``` SUBSTR(string, start_position, [length]) ``` 其中: - `string` 是要截取的字符串。 - `start_position` 是要截取的字符串中第一个字符的位置。注意,字符位置是从 1 开始的,而不是从 0 开始。 - `length` 是要截取的字符的数量。如果省略了该参数,则截取从 `start_position` 开始到字符串末尾的所有字符。 例如,要截取字符串 `'abcdef'` 中从第 3 个字符开始的 2 个字符,可以使用以下查询: ``` SELECT SUBSTR('abcdef', 3, 2) FROM dual; ``` 该查询将返回字符串 `'cd'`。 注意:如果指定的 `start_position` 或 `length` 超出了字符串的长度,则 `SUBSTR` 函数会返回 NULL。 ### 回答2: Oracle提供了多个函数用于截取字符串。以下是常用的几个截取字符串函数: 1. SUBSTR函数:SUBSTR函数用于返回一个字符串的子串。它接受三个参数,分别是原字符串、起始位置和子串的长度。例如,SUBSTR('Hello World', 4, 5)将返回"lo Wo",因为它从第4个字符开始,截取长度为5的子串。 2. INSTR函数:INSTR函数用于返回一个字符串在另一个字符串中的位置。它接受三个参数,分别是原字符串、要查找的字符串和起始位置。例如,INSTR('Hello World', 'World')将返回7,因为"World"在原字符串中的起始位置是第7个字符。 3. REPLACE函数:REPLACE函数用于将一个字符串中的指定子串替换为另一个字符串。它接受三个参数,分别是原字符串、要替换的子串和替换后的子串。例如,REPLACE('Hello World', 'World', 'Oracle')将返回"Hello Oracle",因为它将原字符串中的"World"替换为"Oracle"。 4. REGEXP_SUBSTR函数:REGEXP_SUBSTR函数用于根据正则表达式从一个字符串中提取子串。它接受三个参数,分别是原字符串、正则表达式和匹配模式。例如,REGEXP_SUBSTR('Hello World', 'W.ld', 1, 1)将返回"World",因为它使用正则表达式'W.ld'从原字符串中提取出匹配的子串。 这些函数提供了灵活的字符串截取和处理功能,可以根据具体的需求选择合适的函数来使用。 ### 回答3: Oracle数据库中提供了多种截取字符串函数,常用的有SUBSTR和SUBSTRING函数。 SUBSTR函数用于截取字符串中指定位置开始的子字符串,其语法为SUBSTR(源字符串, 起始位置, 截取长度)。例如,SUBSTR('Hello, world!', 7, 5)将返回"world",因为它从源字符串的第七个位置开始截取了5个字符。 SUBSTRING函数也可以实现字符串的截取,其语法为SUBSTRING(源字符串, 起始位置, 截取长度)。与SUBSTR函数不同的是,SUBSTRING函数的起始位置是从1开始计数的。例如,SUBSTRING('Hello, world!', 2, 4)将返回"ello",因为它从源字符串的第二个位置开始截取了4个字符。 除了SUBSTR和SUBSTRING函数,Oracle还提供了其他一些用于字符串截取的函数,例如INSTR函数可以用于查找字符串中指定子串的位置,而使用REGEXP_SUBSTR函数可以使用正则表达式进行更为灵活的字符串截取操作。 总之,Oracle截取字符串函数提供了多种方式来满足不同的截取需求,我们可以根据具体的业务场景选择合适的函数来使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值