如何從包含「I」和「P」之間的文件名「I1P706.jpg」獲取值,因此在這種情況下應該是「1」? 一般該值的長度可以超過1 sumbol如何從字符串名稱獲取值?
回答
獲得的I和P指標,然後得到開始iIndex的子(可能需要+ 1)的數我和P(這是P - I)之間的字符。
string myString = "I1P706.jpg"
int iIndex = myString.IndexOf("I");
int pIndex = myString.IndexOf("P");
string betweenIAndP = myString.Substring(iIndex + 1, pIndex - iIndex - 1);
這些都是C#中字符串操作的好方法 – 2011-12-23 11:17:53
爲什麼downvotes? – ThePower 2011-12-23 11:18:03
每個答案都沒有留下評論而從某人下來。這個答案不會返回所需的結果'1''''1'。 – 2011-12-23 11:31:38
string input = "I1P706.jpg";
// Get the characters by specifying the limits
string sub = input.Substring(1,3);
在這種情況下,輸出將是1P
。
您也可以你slice
功能
的Peaceful
Slice(1,4)
將返回eac
但它的長度可以超過1 – revolutionkpi 2011-12-23 11:11:58
@revolutionkpi - 你沒有這麼說。 – Oded 2011-12-23 11:12:25
使用正則表達式:
var r = new Regex(@"I(\d+)P.*");
var match = r.Match(input, RegexOptions.IgnoreCase);
if (match.Success)
{
int number = 0; // set a default value
int.TryParse(match.Groups[1].Value, out number);
Console.WriteLine(number);
}
我猜你想這兩個數字:
using System.Text.RegularExpressions;
RegEx rx(@"I(\d+)P(\d+)\.jpg");
Match m = rx.Match("I1P706.jpg");
if(m.Success)
{
// m.Groups[1].Value contains the first number
// m.Groups[2].Value contains the second number
}
else
{
// not found...
}
var input = "I1P706.jpg";
var indexOfI = input.IndexOf("I");
var result = input.Substring(indexOfI + 1, input.IndexOf("P") - indexOfI - 1);
這個正則表達式將會給你的所有字符我只是一個P的,忽略案件。這將允許I和P之間的數字增長。
例
string fileName = "I1222222P706.jpg";
Regex r = new Regex(@"(?<=I)(.*?)(?=P)",
RegexOptions.Singleline | RegexOptions.IgnoreCase);
var result = r.Split(fileName).GetValue(1);
- 1. 如何從它的字符串名稱獲取資源的值
- 2. 獲取名稱(字符串)
- 3. 如何從python字符串獲取命名變量的名稱
- 4. 如何從任何字符串url獲取網站的名稱
- 5. 作爲字符串的變量名稱,如何獲取值?
- 6. 如何從字符串獲取域名
- 7. 如何從字符串獲取域名?
- 8. 如何從字符串獲取類名
- 9. C#:如何從字符串XElement獲取名稱(帶前綴)?
- 10. 如何從xslt中的字符串獲取所需的名稱?
- 11. 如何通過名稱引用從XML獲取字符串?
- 12. 如何從JCalendar獲取JMonthChooser上的字符串月份名稱
- 13. 如何從ISO日期字符串獲取時區名稱?
- 14. 從字符串中獲取名稱變量值
- 15. 從名稱在字符串上的整數中獲取值
- 16. 如何從json字符串獲取值?
- 17. 如何從字符串中獲取值?
- 18. 如何從json字符串獲取值?
- 19. 如何從字符串庫名稱中獲取jar庫名稱和版本?
- 20. 按名稱獲取元素字符串
- 21. 獲取子字符串圖像名稱
- 22. 獲取字符串的常量名稱
- 23. 如何從另一個字符串獲取字符串值?
- 24. 如何從另一個字符串獲取字符串值
- 25. 如何從包含「,」的字符串獲取字符串值?
- 26. 如何從字符串變量名稱?
- 27. 任何方式從字符串值中提取變量名稱?
- 28. 從字符串URL獲取域名和頁面名稱
- 29. 從字符串名稱
- 30. vb net:從xml獲取字符串,如何獲取三個值?
將它始終處於那個位置? – 2011-12-23 11:09:44
string [1] ........... – 2011-12-23 11:10:40
你試過了什麼?它只會是1位數嗎?總是在'I'和'P'之間?這些將始終在字符串的開頭嗎?你需要什麼數據類型作爲結果?一個字符串?詮釋?還有別的嗎? – Oded 2011-12-23 11:10:41