Swift - 根据阿拉伯组合字符拆分文本

Swift - Split text based on arabic combined characters

亲爱的,

我有这样一句阿拉伯语句子

أكل الولد التفاحة

如何根据未连接的字符将句子拆分成这样:

أ- كل

ا- لو- لد

ا- لتفا-

我用-来解释我的意思。 我只需要根据那个

将文本拆分成数组

我如何使用 swift 代码 ios 来做到这一点?

更新: 我不在乎空间。 例如,“أكل”是一个单词,不包含 spaces.I 要根据未连接的字符进行拆分。 所以 "أكل" 由两个对象组成: "أ" 和 "كل"

الولد:三个对象“ا”、“لو”和“لد”

试试这个:

"أكل الولد التفاحة".map {String([=10=])}

使用下面的代码:

let a = "أكل الولد التفاحة".split(separator: " ")

您可以使用替换出现功能将空格替换为“-”。

let text = "أكل الولد التفاحة".replacingOccurrences(of: " ", with: "-", options: NSString.CompareOptions.literal, range: nil) ?? ""

我不知道接受的答案如何帮助解决问题。

Apple 已经提供了 Natural Language 框架来处理这样更值得信赖的事情

When you work with natural language text, it’s often useful to tokenize the text into individual words. Using NLTokenizer to enumerate words, rather than simply splitting components by whitespace, ensures correct behavior in multiple scripts and languages. For example, neither Chinese nor Japanese uses spaces to delimit words.

这是例子

let text = """
All human beings are born free and equal in dignity and rights.
They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood.
"""

let tokenizer = NLTokenizer(unit: .word)
tokenizer.string = text

tokenizer.enumerateTokens(in: text.startIndex..<text.endIndex) { tokenRange, _ in
    print(text[tokenRange])
    return true
}

这是 Apple docs

的 link

希望对您有所帮助

有两个框你可以先点击进去。内容自动粘贴点击转换。输出数据自动复制了我用于此古兰经的空格

<h1>Allah</h1>
<center>
<textarea id="field" onclick="paste(this)" style="font-size: xxx-large;min-width: 90%;     min-height: 200px;"> </textarea>
<center>
</center>
</br>
<textarea id="field2" style="font-size: xxx-large;min-width: 95%;     min-height: 200px;"> </textarea>
</center>
<center>
<br>

<button onclick="myFunction()"  style="font-size: xx-large;min-width: 20%;">Convert</button>
</center>

<script >

function myFunction(){
var string = document.getElementById("field").value;

// Option 1
string.split('');

// Option 2
console.log(string);

// Option 3
Array.from(string);

// Option 4
var bb = Object.assign([], string);
console.log(bb);

    cleanArray = bb.filter(function () { return true });
    
      var filtered = bb.filter(function (el) { 
                return el != null; });

console.log(filtered);
var bb = bb.toString();

console.log(bb);
bb  = bb.replace(",","");

var stringWithoutCommas = bb.replace(/,/g, ' ');

console.log(stringWithoutCommas);

document.execCommand(stringWithoutCommas)
document.getElementById("field2").value = stringWithoutCommas;

  var copyTextarea = document.querySelector('#field2');
  copyTextarea.focus();
  copyTextarea.select();

  try {
    var successful = document.execCommand('copy');
    var msg = successful ? 'successful' : 'unsuccessful';
    console.log('Copying text command was ' + msg);
  } catch (err) {
    console.log('Oops, unable to copy');
  }
};

/*
var copyTextareaBtn = document.querySelector('#newr');

copyTextareaBtn.addEventListener('click', function(event) {
  var copyTextarea = document.querySelector('#field2');
  copyTextarea.focus();
  copyTextarea.select();

  try {
    var successful = document.execCommand('copy');
    var msg = successful ? 'successful' : 'unsuccessful';
    console.log('Copying text command was ' + msg);
  } catch (err) {
    console.log('Oops, unable to copy');
  }
});
*/
async function paste(input) {
document.getElementById("field2").value = "";
  const text = await navigator.clipboard.readText();
  input.value = text;
}

</script>