c# Substring()包括当前的嘛
时间: 2023-10-23 10:09:44 浏览: 32
在 C# 中,`Substring()` 方法包括第一个参数指定的索引位置所对应的字符。也就是说,如果指定了起始索引位置,那么截取的字符串中就包括这个位置上的字符。
例如,对于下面的字符串:
```csharp
string str = "hello world!";
```
如果我们使用 `Substring()` 方法从索引 6 开始截取 5 个字符:
```csharp
string subStr = str.Substring(6, 5);
```
则截取到的字符串为 `"world"`,包括了索引 6 上对应的字符 `"w"`。
需要注意的是,如果指定的起始索引位置超过了字符串的长度,或者指定的截取长度超过了字符串的剩余长度,则会导致 `Substring()` 方法产生异常。因此,在使用该方法时需要保证传入的参数合法。
相关问题
c# StringSearch 用法
C# 中的 StringSearch 是一个用于字符串匹配的类,它提供了多种字符串查找和替换的方法。下面是一些常用的 StringSearch 方法及其用法:
1. IndexOf(string value):查找指定字符串在当前字符串中第一次出现的位置。
```csharp
string str = "hello world";
int index = str.IndexOf("world"); // index = 6
```
2. LastIndexOf(string value):查找指定字符串在当前字符串中最后一次出现的位置。
```csharp
string str = "hello world";
int index = str.LastIndexOf("l"); // index = 9
```
3. Contains(string value):判断当前字符串是否包含指定字符串。
```csharp
string str = "hello world";
bool result = str.Contains("world"); // result = true
```
4. Replace(string oldValue, string newValue):将当前字符串中所有的 oldValue 替换为 newValue。
```csharp
string str = "hello world";
string newStr = str.Replace("world", "universe"); // newStr = "hello universe"
```
5. Split(char[] separator):将当前字符串按照指定字符数组分割成字符串数组。
```csharp
string str = "hello,world";
string[] words = str.Split(new char[] { ',' }); // words = ["hello", "world"]
```
6. Substring(int startIndex, int length):截取当前字符串的一部分,从 startIndex 开始,长度为 length。
```csharp
string str = "hello world";
string subStr = str.Substring(6, 5); // subStr = "world"
```
7. ToLower() / ToUpper():返回当前字符串的小写 / 大写形式。
```csharp
string str = "Hello World";
string lowerStr = str.ToLower(); // lowerStr = "hello world"
string upperStr = str.ToUpper(); // upperStr = "HELLO WORLD"
```
以上是 StringSearch 类的一些常用方法及其用法,希望能对你有所帮助。
c# farpoint 单元格 wrodwrap 避免 括号换行
如果您使用的是 FarPoint 的 WinForms 控件,可以考虑在单元格中使用自定义绘制来解决此问题。您可以从 FpSpread 控件的 CellType 属性中选择 Custom,然后在程序中实现自定义绘制逻辑。
在自定义绘制逻辑中,您可以遍历单元格文本中的每个字符,并在必要时手动插入换行符(例如在左括号和右括号之间不进行换行)。然后,您可以使用 Graphics 对象进行文本绘制,并将其输出到单元格中。
下面是一些示例代码,用于在 FarPoint FpSpread 控件的自定义单元格类型中实现包含自定义绘制逻辑的单元格:
```csharp
using FarPoint.Win.Spread.CellType;
using FarPoint.Win.Spread.DrawingSpace;
using System.Drawing;
public class CustomWrapCellType : TextCellType
{
public override void Paint(Graphics g, Rectangle r, object value)
{
string text = value as string;
if (text == null) return;
// 计算行高和列宽
int lineHeight = (int)g.MeasureString(text, this.Font).Height;
int columnWidth = r.Width;
// 遍历每个字符并插入换行符
for (int i = 0; i < text.Length; i++)
{
char c = text[i];
if (c == '(' && i < text.Length - 1 && text[i + 1] == ')')
{
// 避免在括号内部换行
continue;
}
if (c == ' ' && i < text.Length - 1 && text[i + 1] == '(')
{
// 避免在括号前面换行
continue;
}
if (c == ')' && i > 0 && text[i - 1] == '(')
{
// 避免在括号后面换行
continue;
}
if (c == '\r' || c == '\n')
{
// 如果文本中已经包含换行符,则不需要再插入
continue;
}
if (g.MeasureString(text.Substring(0, i + 1), this.Font).Width > columnWidth)
{
// 当前行已经超出列宽,需要插入换行符
text = text.Insert(i, Environment.NewLine);
i += Environment.NewLine.Length;
}
}
// 绘制文本
using (StringFormat sf = new StringFormat())
{
sf.Alignment = StringAlignment.Near;
sf.LineAlignment = StringAlignment.Near;
sf.FormatFlags = StringFormatFlags.NoWrap;
RectangleF rect = new RectangleF(r.Left, r.Top, columnWidth, lineHeight);
g.DrawString(text, this.Font, new SolidBrush(this.ForeColor), rect, sf);
}
}
}
```
使用以上代码,您可以将包含自定义绘制逻辑的单元格类型应用于 FpSpread 控件中特定的单元格。例如:
```csharp
FpSpread spread = new FpSpread();
SheetView sheet = spread.ActiveSheet;
CustomWrapCellType cellType = new CustomWrapCellType();
sheet.Cells[0, 0].CellType = cellType;
sheet.Cells[0, 0].Value = "这是一段包含括号的文本(不会换行)";
sheet.Cells[1, 0].CellType = cellType;
sheet.Cells[1, 0].Value = "这是一段包含括号的文本,(但是括号前后不会换行)";
sheet.Cells[2, 0].CellType = cellType;
sheet.Cells[2, 0].Value = "这是一段较长的文本,将会自动换行,但不会在括号内部换行。如果文本中已经包含换行符,则不会再插入。";
```
以上代码将在 FpSpread 控件的第一列中显示三个单元格,每个单元格包含不同形式的文本,并使用自定义绘制逻辑进行换行处理。