c#删除string最后的".cpp"
时间: 2023-07-19 10:33:01 浏览: 45
可以使用 `Substring` 方法和 `EndsWith` 方法来实现删除一个字符串末尾的指定子串。示例代码如下:
```csharp
string str = "example.cpp";
if (str.EndsWith(".cpp"))
{
str = str.Substring(0, str.Length - 4);
}
```
这里的 `EndsWith` 方法用于判断字符串 `str` 是否以指定的后缀 `.cpp` 结尾,如果是,则使用 `Substring` 方法删除后缀,得到新的字符串。
相关问题
C#.net Process调用g++.exe执行.\g++.exe file.cpp -o file
可以使用Process类来调用g++编译器来编译一个C++源文件。可以参考下面的代码:
```
string compilerPath = "g++.exe";
string sourceFile = "file.cpp";
string outputFile = "file.exe";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = compilerPath;
startInfo.Arguments = $"{sourceFile} -o {outputFile}";
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
if (process.ExitCode == 0)
{
Console.WriteLine("Compilation succeeded.");
}
else
{
Console.WriteLine("Compilation failed. Output:");
Console.WriteLine(output);
}
```
上述代码将调用g++编译器来编译file.cpp源文件,并生成名为file.exe的可执行文件。如果编译成功,将输出"Compilation succeeded.",否则将输出"Compilation failed. Output:"和编译器的输出信息。
C# F:\\code\\hstrade20\\trunk\\SourcesEx\\hsoffer\\offer_biz_bjs\\offer_biz_bjs.cpp 截取offer_biz_bjs.cpp 中的 offer_biz_bjs
C# 是一种通用的面向对象编程语言,由微软公司开发。它具有简单、现代化和安全的特性,广泛应用于Windows平台的软件开发。C#支持多种编程范式,包括面向对象编程、泛型编程、事件驱动编程等。
关于截取文件路径中的文件名,你可以使用C#的Path类来实现。具体的代码如下:
```csharp
using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = @"F:\code\hstrade20\trunk\SourcesEx\hsoffer\offer_biz_bjs\offer_biz_bjs.cpp";
string fileName = Path.GetFileNameWithoutExtension(filePath);
Console.WriteLine(fileName);
}
}
```
上述代码中,我们使用Path类的GetFileNameWithoutExtension方法来获取文件路径中的文件名(不包括扩展名),然后将其输出到控制台。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)