我想要字符串处理...... [英] I want string processing...
问题描述
我有字符串......
---------- 56628885466 ---- 655277772323777 ---
我想处理这个字符串..
1.i希望字符串字符串分开如
56628885466
655277772323777
2.找出每个字符串中的最高值,即8 n 7
3.i想要用 - 代替最高编号以外的其他编号
字符串看起来像
---- ---------- 888 ------------ 7777 ---- 777 ---
4.我希望通过los / 2找到字符串的中间值并再次用 - 替换它
-------------- 8- -------------- 7 ------ 7 ----
5.然后我想知道数组价值指数
我尝试过:
I have string...
----------56628885466----655277772323777---
I want to process this string..
1.i want string string to split like
56628885466
655277772323777
2.Find the highest value in each string i.e., 8 n 7
3.i want to replace other than highest no with "-"
string will look like
--------------888------------7777----777---
4. The i want find the middle value of string by los/2 and again replace it by "-"
--------------8---------------7------7----
5. then i want to know the array index of the value with value
What I have tried:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim s As String = IO.File.ReadAllText("C:\Users\mks\Desktop\Newfolder\test.sp1")
Dim r As String = Regex.Replace(s.ToString, "----", "-")
Dim p As String = Regex.Replace(r.ToString, "--", "-")
Dim t As String = Regex.Replace(p.ToString, "--", "-")
Dim elements() As String = Regex.Split(t, "-")
For Each element In elements
Next
Dim rp As String
rp = elements(0)
Console.WriteLine(rp)
End Sub
推荐答案
用c#编写,获取逻辑并在vb中执行相同操作,您可能会发现许多在线代码转换器将c#转换为vb
written in c#, get the logic and do the same in vb, you may find many online code converters to convert c# to vb
using System;
using System.Linq;
namespace CP
{
class Program
{
static void Main(string[] args)
{
string input = "----------56628885466----655277772323777---";
string[] parts = input.Split(new char[] { '-' }, StringSplitOptions.None);
string final = "";
foreach (string part in parts)
{
if (part == "")
final += "-";
else
{
var max = part.Max();
string temp = part;
foreach (char c in part)
{
if (c != max)
temp = temp.Replace(c, '-');
}
string temp1 = "";
foreach (string item in temp.Split('-'))
{
if (item == "")
temp1 += item;
else
temp1 += GetMiddle(item);
}
final += temp1;
}
}
Console.WriteLine(final); //"-----------8------7--7----"
}
private static string GetMiddle( string item)
{
var a = item.Length / 2;
string temp = "";
for (int i = 0; i < item.Length; i++)
{
if (i == a)
temp += item[0];
else
temp += "-";
}
return temp;
}
}
}
在这里您可以找到解决方案从Karthik转换为VB:
Here you find the Solution from Karthik converted to VB :
Imports System
Imports System.Linq
Namespace CP
Class Program
Private Shared Sub Main(args As String())
Dim input As String = "----------56628885466----655277772323777---"
Dim parts As String() = input.Split(New Char() {"-"C}, StringSplitOptions.None)
Dim final As String = ""
For Each part As String In parts
If part = "" Then
final += "-"
Else
Dim max = part.Max()
Dim temp As String = part
For Each c As Char In part
If c <> max Then
temp = temp.Replace(c, "-"C)
End If
Next
Dim temp1 As String = ""
For Each item As String In temp.Split("-"C)
If item = "" Then
temp1 += item
Else
temp1 += GetMiddle(item)
End If
Next
final += temp1
End If
Next
Console.WriteLine(final)
'"-----------8------7--7----"
End Sub
Private Shared Function GetMiddle(item As String) As String
Dim a = item.Length / 2
Dim temp As String = ""
For i As Integer = 0 To item.Length - 1
If i = a Then
temp += item(0)
Else
temp += "-"
End If
Next
Return temp
End Function
End Class
End Namespace
这篇关于我想要字符串处理......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!