如何将字符串插入其他符合条件的字符串? [英] How can I insert a string into other certain strings that meet a criteria?

查看:24
本文介绍了如何将字符串插入其他符合条件的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在设计一个程序,该程序接受用户输入,将其(如果是英文)转换为 Pig Latin,反之亦然.当您单击菜单按钮或按 F3 时,就会发生转换.这是当前的特定代码块:

I'm currently designing a program that takes user input, converts that (if in English) to Pig Latin and vice versa. When you click on the menu button or press F3, the conversion happens. This is what the specific block of code is currently:

For i As Integer = 0 To words.Length - 1
    If inputTextBox.Text Like "" Then
        'i = words(i).Insert(i, "--way")
            outputTextBox.Text = words(i)
    End If
Next

我想要做的是,如果里面的文本符合条件,例如输入文本框中的输入是否以 a、e、i、o 或 u 开头,那么该词应该在结尾.我通过说:

What I want to do is, if text inside meets criteria such as if input inside the input text box starts with a, e, i, o or u, that word should get a "--way" tagged on at the end. I got the vice versa part working by saying:

If inputTextBox.Text Like "--way" Then
        MsgBox("Cannot convert, already in English.")
End If

但我不确定如何编码以检测是否在任何地方都没有--way".另一个转换部分是,如果一个词不以 a、e、i、o 或 u 开头,但在词中包含 a、e、i、o、u 或 y,则这些词会得到:

but I'm not sure how to code as to detect if there is NO "--way" anywhere. Another conversion part is if a word does NOT begin with a, e, i, o or u but contains a, e, i, o, u or y WITHIN the word, those words get:

单词末尾的破折号 (-)

a dash (-) at the end of the word

单词的第一个字母移动到单词的末尾(循环直到第一个字母是 a、e、i、o、u 或 y)

the first letter of the word gets moved to the end of the word (this loops until the first letter is a, e, i, o, u or y)

这个词的末尾有一个ay"

the word gets an "ay" at the end

所以 Ted 变成了ed-tay".

So Ted becomes "ed-tay."

总而言之,我的问题是:

To sum it all up, my questions are:

1 - 如何设置我的 Like 命令以将 --way 添加到特定单词?

1 - How can I set up my Like command to add --way to specific words?

2 - 如何检测文本框中没有 --way 的单词?

2 - How can I detect for words inside the text box that do NOT have --way in them?

推荐答案

是的,有一种方法可以做任何事情,但前提是你知道正确的方法.

Yes, there is a way to do everything but if you know the right way.

要检测单个字符串中的单词,您需要将字符串部分断开(划分).在过去的两天里,我一直在做这个项目.

For detecting words in single strings you need to break(divide) the strings in part. In past 2 days, I had worked on this project.

您应该首先将字符串分成列(块),然后分别阅读每个单词.要分析字符串,请使用此示例.虽然这种方法很长,但您也可以尝试其他方法,例如使用数组或列表.例如:

You should first divide the String into columns(pieces), then read each and every word separately. For analysing a string use this sample. Although this method is long you can try other ways as well like you can use array or list. E.g:

`Imports System.IO
Public Class Form1

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim MyWord As String = TextBox1.Text   'This is the input string by user.
    Dim writer As New StreamWriter(System.IO.Directory.GetCurrentDirectory + "/fp.txt") 'Filepath = for e.g. =  D:\Myfile.txt
    Dim i As Double = 0
    Dim MyWordlength As Double = MyWord.length
    Dim column As String
    While i < MyWordlength
        column = MyWord.Chars(i)
        writer.WriteLine(column)
        i = i + 1
    End While
    writer.close()
    writer.Dispose()
    ' Then set code to analyse
    Dim i2 As Double = 0
    Dim firstcharacter As String = ReadALine(System.IO.Directory.GetCurrentDirectory + "/fp.txt", GetNumberofLines(System.IO.Directory.GetCurrentDirectory + "/fp.txt"), 0) 'Set File Path (any you wish but valid)
    'char.
    If firstcharacter = "a" Or firstcharacter = "A" Or firstcharacter = "e" Or firstcharacter = "E" Or firstcharacter = "i" Or firstcharacter = "I" Or firstcharacter = "o" Or firstcharacter = "O" Or firstcharacter = "U" Or firstcharacter = "u" Then
        MsgBox("Should not start with a,e,i,o,u.")
    End If
    'For analysing remaining words.
    Dim linec As String
    Dim check As Boolean = False
    While i2 <= MyWord.Length
        linec = ReadALine("D:\Myfile.txt", GetNumberofLines(System.IO.Directory.GetCurrentDirectory + "/fp.txt"), i2)
        If linec <> "a" Or linec <> "i" Or linec <> "e" Or linec <> "o" Or linec <> "u" Or linec <> "A" Or linec <> "E" Or linec <> "I" Or linec <> "O" Or linec <> "U" Then
            check = False
            i2 = i2 + 1
        Else
            check = True
        End If
    End While
    If check = True Then
    Else
        MsgBox("You does'not use any vowel between all letters.")
    End If
End Sub
Public Function ReadALine(ByVal File_Path As String, ByVal TotalLine As Integer, ByVal Line2Read As Integer) As String
    Dim buffer As Array
    Dim line As String
    If TotalLine <= Line2Read Then
        Return "0"
    End If
    buffer = file.ReadAllLines(File_Path)
    line = buffer(Line2Read)
    Return line
End Function
Public Function GetNumberofLines(ByVal File_path As String) As Integer
    Dim sr As New StreamReader(File_path)
    Dim numberoflines As Integer
    Do While sr.Peek >= 0
        sr.ReadLine()
        numberoflines += 1
    Loop
    Return numberoflines
    sr.Close()
    sr.Dispose()
End Function
End Class

这篇关于如何将字符串插入其他符合条件的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆