بیا این کد پیدا کردن عدد
For i = 1 To Len(Text1.Text)
If IsNumeric(Mid(Text1, i, 1)) = True Then
If i - ibuf = 1 Then
ibuf = i
buf = buf & Mid(Text1, i, 1)
Else
ibuf = i
buf = buf & ","
buf = buf & Mid(Text1, i, 1)
End If
End If
Next i
buf = Mid$(buf, 2)
Dim arr() As String
arr() = Split(buf, ",", , vbTextCompare)
'Print all array to text2
For i = 0 To UBound(arr())
Text2 = Text2 & "arr(" & i & ") : " & arr(i) & vbNewLine
Next
اینم عکس محیط برنامه
حالا که اعداد رو در آرایه بدست آوردیم میتوانیم برای جایگزین کردن حروف آنها در متن از کد زیر استفاده کنیم
For i = 0 To UBound(arr())
call Replace(text1,arr(i),numtostr(arr(i)),1,-1,vbTextCompare)
Next
که تابع numtostr همون تابع برگنداندن حروف از عدد است که شما گفتید برای آن مشکلی ندارید
اگر مشکلی داشتید ایمیل بنمایید
[email protected]
اگر هم برنامه رو در زبان دیگری میخواهی میتوانی از الگوریتم موجود استفاده کنی
اگر نتونستی الگوریتم رو استخراج کنی بگو تا تفصیرش کنم
کد بالا به زبان vb بود حالا برای c# از کد زیر استفاده کن
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static String text1 = "وی در سن 17 سالگی سمت منشی گری دیوان همایون را داشت و 2 سال بعد بمنصب مترجمی باب عالی رسید ";
static String buf;
static void Main(string[] args)
{
int ibuf=0;
for (int i = 1; i < text1.Length; i++)
{
if (char.IsDigit(text1,i) == true)
{
if (i-ibuf==1)
{
ibuf = i;
buf = buf + text1.Substring(i,1);
}
else
{
ibuf = i;
buf = buf + ",";
buf = buf + text1.Substring(i, 1);
}
}
}
buf = buf.Substring(1);
Console.WriteLine(buf);
//string[] arr = buf.Split(",");
/*foreach (string word in arr)
{
Console.WriteLine(word);
}*/
Console.ReadKey();
}
}
}
خروجی کار هم عکس پایین است
که میتونی با تابع replace به جای عدد ها حروف آنرا بگذارید